MythTV  master
pbs.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: UTF-8 -*-
3 # ----------------------
4 # Name: pbs.py
5 # Python Script
6 # Author: R.D. Vaughan
7 # Purpose:
8 # This python script is intended to perform a mashup of various PBS 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/pbs.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__ ="PBS";
24 __mashup_title__ = "pbs"
25 __author__="R.D. Vaughan"
26 __version__="v0.10"
27 # 0.10 Initial development
28 
29 __usage_examples__ ='''
30 (Option Help)
31 > ./pbs.py -h
32 Usage: ./pbs.py -hduvlST [parameters] <search text>
33 Version: v0.1.0 Author: R.D.Vaughan
34 
35 For details on the MythTV Netvision plugin see the wiki page at:
36 http://www.mythtv.org/wiki/MythNetvision
37 
38 Options:
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 > ./pbs.py -v
57 <grabber>
58  <name>PBS</name>
59  <author>R.D. Vaughan</author>
60  <thumbnail>pbs.png</thumbnail>
61  <command>pbs.py</command>
62  <type>video</type>
63  <description>Discover award-winning programming right at your fingertips on PBS Video. Catch the episodes you may have missed and watch your favorite shows whenever you want.</description>
64  <version>v0.10</version>
65  <search>true</search>
66  <tree>true</tree>
67 </grabber>
68 
69 > ./pbs.py -S "Dinosaurs"
70 
71 > ./pbs.py -T
72 '''
73 __search_max_page_items__ = 20
74 __tree_max_page_items__ = 20
75 
76 import sys, os
77 
78 
79 class 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, unicode):
91  try:
92  self.out.write(obj.encode(self.encoding))
93  except IOError:
94  pass
95  else:
96  try:
97  self.out.write(obj)
98  except IOError:
99  pass
100 
101  def __getattr__(self, attr):
102  """Delegate everything but write to the stream"""
103  return getattr(self.out, attr)
104 sys.stdout = OutStreamEncoder(sys.stdout, 'utf8')
105 sys.stderr = OutStreamEncoder(sys.stderr, 'utf8')
106 
107 
108 # Used for debugging
109 #import nv_python_libs.common.common_api
110 try:
111  '''Import the common python class
112  '''
113  import nv_python_libs.common.common_api as common_api
114 except Exception, e:
115  sys.stderr.write('''
116 The subdirectory "nv_python_libs/common" containing the modules mashups_api.py and
117 mashups_exceptions.py (v0.1.3 or greater),
118 They should have been included with the distribution of MythNetvision
119 Error(%s)
120 ''' % e)
121  sys.exit(1)
122 
123 if common_api.__version__ < '0.1.3':
124  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__)
125  sys.exit(1)
126 
127 # Used for debugging
128 #import nv_python_libs.pbs.pbs_api as target
129 try:
130  '''Import the python mashups support classes
131  '''
132  import nv_python_libs.pbs.pbs_api as target
133 except Exception, e:
134  sys.stderr.write('''
135 The subdirectory "nv_python_libs/pbs" containing the modules pbs_api and
136 pbs_exceptions.py (v0.1.0 or greater),
137 They should have been included with the distribution of pbs.py.
138 Error(%s)
139 ''' % e)
140  sys.exit(1)
141 if target.__version__ < '0.1.0':
142  sys.stderr.write("\n! Error: Your current installed pbs_api.py version is (%s)\nYou must at least have version (0.1.0) or higher.\n" % target.__version__)
143  sys.exit(1)
144 
145 # Verify that the main process modules are installed and accessible
146 try:
147  import nv_python_libs.mainProcess as process
148 except Exception, e:
149  sys.stderr.write('''
150 The python script "nv_python_libs/mainProcess.py" must be present.
151 Error(%s)
152 ''' % e)
153  sys.exit(1)
154 
155 if process.__version__ < '0.2.0':
156  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__)
157  sys.exit(1)
158 
159 if __name__ == '__main__':
160  # No api key is required
161  apikey = ""
162  # Set the base processing directory that the grabber is installed
163  target.baseProcessingDir = os.path.dirname( os.path.realpath( __file__ ))
164  # Make sure the target functions have an instance of the common routines
165  target.common = common_api.Common()
166  main = process.mainProcess(target, apikey, )
167  main.grabberInfo = {}
168  main.grabberInfo['title'] = __title__
169  main.grabberInfo['command'] = u'pbs.py'
170  main.grabberInfo['mashup_title'] = __mashup_title__
171  main.grabberInfo['author'] = __author__
172  main.grabberInfo['thumbnail'] = 'pbs.png'
173  main.grabberInfo['type'] = ['video', ]
174  main.grabberInfo['desc'] = u"Discover award-winning programming right at your fingertips on PBS Video. Catch the episodes you may have missed and watch your favorite shows whenever you want."
175  main.grabberInfo['version'] = __version__
176  main.grabberInfo['search'] = target.common.checkIfDBItem('dummy', {'feedtitle': __title__, })
177  main.grabberInfo['tree'] = True
178  main.grabberInfo['html'] = False
179  main.grabberInfo['usage'] = __usage_examples__
180  main.grabberInfo['SmaxPage'] = __search_max_page_items__
181  main.grabberInfo['TmaxPage'] = __tree_max_page_items__
182  main.main()
def write(self, obj)
Definition: pbs.py:88
def __init__(self, outstream, encoding=None)
Definition: pbs.py:81
def __getattr__(self, attr)
Definition: pbs.py:101