MythTV master
tedtalks.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: UTF-8 -*-
3# ----------------------
4# Name: tedtalks.py
5# Python Script
6# Author: R.D. Vaughan
7# Purpose:
8# This python script is intended to perform a mashup of various TedTalks Internet media sources
9# for the MythTV Netvision plugin. It follows the MythTV Netvision grabber standards.
10#
11# Command example:
12# See help (-u and -h) options
13#
14# Design:
15# 1) Read "~/.mythtv/MythNetvision/userGrabberPrefs/tedtalks.xml" configuration file
16# 2) Input the sources that are marked as enabled
17# 3) Process the results and display to stdout
18#
19#
20# License:Creative Commons GNU GPL v2
21# (http://creativecommons.org/licenses/GPL/2.0/)
22#-------------------------------------
23__title__ ="TedTalks";
24__mashup_title__ = "tedtalks"
25__author__="R.D. Vaughan"
26__version__="v0.10"
27# 0.10 Initial development
28
29__usage_examples__ ='''
30(Option Help)
31> ./tedtalks.py -h
32Usage: ./tedtalks.py -hduvlST [parameters] <search text>
33Version: v0.1.0 Author: R.D.Vaughan
34
35For details on the MythTV Netvision plugin see the wiki page at:
36http://www.mythtv.org/wiki/MythNetvision
37
38Options:
39 -h, --help show this help message and exit
40 -d, --debug Show debugging info (URLs, raw XML ... etc, info
41 varies per grabber)
42 -u, --usage Display examples for executing the script
43 -v, --version Display grabber name and supported options
44 -l LANGUAGE, --language=LANGUAGE
45 Select data that matches the specified language fall
46 back to English if nothing found (e.g. 'es' EspaƱol,
47 'de' Deutsch ... etc). Not all sites or grabbers
48 support this option.
49 -p PAGE NUMBER, --pagenumber=PAGE NUMBER
50 Display specific page of the search results. Default
51 is page 1. Page number is ignored with the Tree View
52 option (-T).
53 -S, --search Search for videos
54 -T, --treeview Display a Tree View of a sites videos
55
56> ./tedtalks.py -v
57<grabber>
58 <name>TedTalks</name>
59 <author>R.D. Vaughan</author>
60 <thumbnail>tedtalks.png</thumbnail>
61 <command>tedtalks.py</command>
62 <type>video</type>
63 <description>TED is a small nonprofit devoted to Ideas Worth Spreading.</description>
64 <version>v0.10</version>
65 <search>true</search>
66 <tree>true</tree>
67</grabber>
68
69> ./tedtalks.py -S "Africa"
70
71> ./tedtalks.py -T
72'''
73__search_max_page_items__ = 10
74__tree_max_page_items__ = 10
75
76import sys, os
77import io
78
79class OutStreamEncoder(object):
80 """Wraps a stream with an encoder"""
81 def __init__(self, outstream, encoding=None):
82 self.out = outstream
83 if not encoding:
84 self.encoding = sys.getfilesystemencoding()
85 else:
86 self.encoding = encoding
87
88 def write(self, obj):
89 """Wraps the output stream, encoding Unicode strings with the specified encoding"""
90 if isinstance(obj, str):
91 obj = obj.encode(self.encoding)
92 self.out.buffer.write(obj)
93
94 def __getattr__(self, attr):
95 """Delegate everything but write to the stream"""
96 return getattr(self.out, attr)
97
98if isinstance(sys.stdout, io.TextIOWrapper):
99 sys.stdout = OutStreamEncoder(sys.stdout, 'utf8')
100 sys.stderr = OutStreamEncoder(sys.stderr, 'utf8')
101
102
103# Used for debugging
104#import nv_python_libs.common.common_api
105try:
106 '''Import the common python class
107 '''
108 import nv_python_libs.common.common_api as common_api
109except Exception as e:
110 sys.stderr.write('''
111The subdirectory "nv_python_libs/common" containing the modules mashups_api.py and
112mashups_exceptions.py (v0.1.3 or greater),
113They should have been included with the distribution of MythNetvision
114Error(%s)
115''' % e)
116 sys.exit(1)
117
118if common_api.__version__ < '0.1.3':
119 sys.stderr.write("\n! Error: Your current installed common_api.py version is (%s)\nYou must at least have version (0.1.3) or higher.\n" % target.__version__)
120 sys.exit(1)
121
122# Used for debugging
123#import nv_python_libs.tedtalks.tedtalks_api as target
124try:
125 '''Import the python mashups support classes
126 '''
128except Exception as e:
129 sys.stderr.write('''
130The subdirectory "nv_python_libs/tedtalks" containing the modules tedtalks_api and
131tedtalks_exceptions.py (v0.1.0 or greater),
132They should have been included with the distribution of tedtalks.py.
133Error(%s)
134''' % e)
135 sys.exit(1)
136if target.__version__ < '0.1.0':
137 sys.stderr.write("\n! Error: Your current installed tedtalks_api.py version is (%s)\nYou must at least have version (0.1.0) or higher.\n" % target.__version__)
138 sys.exit(1)
139
140# Verify that the main process modules are installed and accessible
141try:
142 import nv_python_libs.mainProcess as process
143except Exception as e:
144 sys.stderr.write('''
145The python script "nv_python_libs/mainProcess.py" must be present.
146Error(%s)
147''' % e)
148 sys.exit(1)
149
150if process.__version__ < '0.2.0':
151 sys.stderr.write("\n! Error: Your current installed mainProcess.py version is (%s)\nYou must at least have version (0.2.0) or higher.\n" % process.__version__)
152 sys.exit(1)
153
154if __name__ == '__main__':
155 # No api key is required
156 apikey = ""
157 # Set the base processing directory that the grabber is installed
158 target.baseProcessingDir = os.path.dirname( os.path.realpath( __file__ ))
159 # Make sure the target functions have an instance of the common routines
160 target.common = common_api.Common()
161 main = process.mainProcess(target, apikey, )
162 main.grabberInfo = {}
163 main.grabberInfo['enabled'] = True
164 main.grabberInfo['title'] = __title__
165 main.grabberInfo['command'] = 'tedtalks.py'
166 main.grabberInfo['mashup_title'] = __mashup_title__
167 main.grabberInfo['author'] = __author__
168 main.grabberInfo['thumbnail'] = 'tedtalks.png'
169 main.grabberInfo['type'] = ['video', ]
170 main.grabberInfo['desc'] = "TED is a small nonprofit devoted to Ideas Worth Spreading."
171 main.grabberInfo['version'] = __version__
172 main.grabberInfo['search'] = True
173 main.grabberInfo['tree'] = True
174 main.grabberInfo['html'] = False
175 main.grabberInfo['usage'] = __usage_examples__
176 main.grabberInfo['SmaxPage'] = __search_max_page_items__
177 main.grabberInfo['TmaxPage'] = __tree_max_page_items__
178 main.main()
def __init__(self, outstream, encoding=None)
Definition: tedtalks.py:81
def __getattr__(self, attr)
Definition: tedtalks.py:94
def write(self, obj)
Definition: tedtalks.py:88