MythTV  master
bbciplayer.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: UTF-8 -*-
3 # ----------------------
4 # Name: bbciplayer.py
5 # Python Script
6 # Author: R.D. Vaughan
7 # Purpose:
8 # This python script is intended to perform BBC iplayer video lookups for the MythTV Netvision plugin
9 # based on information found on the http://www.bbc.co.uk/iplayer/ website. It
10 # follows the MythTV Netvision grabber standards.
11 #
12 # Command example:
13 # See help (-u and -h) options
14 #
15 # Design:
16 # 1) Read the ".../emml/feConfig.xml"
17 # 2) Check if the CGI Web server should be used or if the script is run locally
18 # 3) Initialize the correct target functions for processing (local or remote)
19 # 4) Process the search or treeview request and display to stdout
20 #
21 #
22 # License:Creative Commons GNU GPL v2
23 # (http://creativecommons.org/licenses/GPL/2.0/)
24 #-------------------------------------
25 __title__ ="BBC iPlayer";
26 __mashup_title__ = "bbcipplayer"
27 __author__="R.D. Vaughan"
28 __version__="0.15"
29 # 0.1.0 Initial development
30 # 0.1.1 Added treeview support
31 # 0.1.2 Convert to detect and use either local or remote processing
32 # 0.13 Change to support xml version information display
33 # 0.14 Added the "command" tag to the xml version information display
34 # 0.15 Converted to new common_api.py library
35 
36 __usage_examples__ ='''
37 (Option Help)
38 > ./bbciplayer.py -h
39 Usage: ./bbciplayer.py -hduvlST [parameters] <search text>
40 Version: v0.1.2 Author: R.D.Vaughan
41 
42 For details on the MythTV Netvision plugin see the wiki page at:
43 http://www.mythtv.org/wiki/MythNetvision
44 
45 Options:
46  -h, --help show this help message and exit
47  -d, --debug Show debugging info (URLs, raw XML ... etc, info
48  varies per grabber)
49  -u, --usage Display examples for executing the script
50  -v, --version Display grabber name and supported options
51  -l LANGUAGE, --language=LANGUAGE
52  Select data that matches the specified language fall
53  back to English if nothing found (e.g. 'es' EspaƱol,
54  'de' Deutsch ... etc). Not all sites or grabbers
55  support this option.
56  -p PAGE NUMBER, --pagenumber=PAGE NUMBER
57  Display specific page of the search results. Default
58  is page 1. Page number is ignored with the Tree View
59  option (-T).
60  -S, --search Search for videos
61  -T, --treeview Display a Tree View of a sites videos
62 
63 > ./bbciplayer.py -v
64 <grabber>
65  <name>BBC iPlayer</name>
66  <author>R.D. Vaughan</author>
67  <thumbnail>bbciplayer.png</thumbnail>
68  <type>video</type>
69  <description>BBC iPlayer is our service that lets you catch up with radio and television programmes from the past week.</description>
70  <version>v0.13</version>
71  <search>true</search>
72  <tree>true</tree>
73 </grabber>
74 
75 > ./bbciplayer.py -S "Doctor Who"
76 <?xml version="1.0" encoding="UTF-8"?>
77 <rss version="2.0" xmlns:amp="http://www.adobe.com/amp/1.0"
78  xmlns:atom="http://www.w3.org/2005/Atom"
79  xmlns:cnettv="http://cnettv.com/mrss/"
80  xmlns:content="http://purl.org/rss/1.0/modules/content/"
81  xmlns:creativecommons="http://backend.userland.com/creativeCommonsRssModule"
82  xmlns:dc="http://purl.org/dc/elements/1.1/"
83  xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:media="http://search.yahoo.com/mrss/">
84  <channel>
85  <title>BBC iPlayer</title>
86  <link>http://www.bbc.co.uk</link>
87  <description>BBC iPlayer is our service that lets you catch up with radio and television programmes from the past week.</description>
88  <numresults>7</numresults>
89  <returned>7</returned>
90  <startindex>7</startindex>
91  <item>
92  <title>Doctor Who - Series 4 - 11. Turn Left</title>
93  <author>British Broadcasting Corporation</author>
94  <pubDate>Wed, 24 Mar 2010 18:35:41 GMT</pubDate>
95  <description>Can Donna and Rose stop the approaching Darkness? (R)</description>
96  <link>http://www.bbc.co.uk/iplayer/episode/b00c7ytx/Doctor_Who_Series_4_Turn_Left/</link>
97  <media:group>
98  <media:thumbnail url="http://node1.bbcimg.co.uk/iplayer/images/episode/b00c7ytx_120_68.jpg"/>
99  <media:content duration="" height="" lang=""
100  url="http://www.bbc.co.uk/iplayer/episode/b00c7ytx/Doctor_Who_Series_4_Turn_Left/" width=""/>
101  </media:group>
102  <rating>0.0</rating>
103  </item>
104 ...
105  <item>
106  <title>Doctor Who Confidential - Series 4 - 13. The End of an Era</title>
107  <author>British Broadcasting Corporation</author>
108  <pubDate>Wed, 24 Mar 2010 18:35:41 GMT</pubDate>
109  <description>The series finale as the Doctor's arch enemy brings the universe to the edge of extinction (R)</description>
110  <link>http://www.bbc.co.uk/iplayer/episode/b00cgphl/Doctor_Who_Confidential_Series_4_The_End_of_an_Era/</link>
111  <media:group>
112  <media:thumbnail url="http://node1.bbcimg.co.uk/iplayer/images/episode/b00cgphl_120_68.jpg"/>
113  <media:content duration="" height="" lang=""
114  url="http://www.bbc.co.uk/iplayer/episode/b00cgphl/Doctor_Who_Confidential_Series_4_The_End_of_an_Era/" width=""/>
115  </media:group>
116  <rating>0.0</rating>
117  </item>
118  </channel>
119 </rss>
120 
121 > ./bbciplayer.py -T
122 <?xml version="1.0" encoding="UTF-8"?>
123 <rss version="2.0" xmlns:amp="http://www.adobe.com/amp/1.0"
124  xmlns:atom="http://www.w3.org/2005/Atom"
125  xmlns:cnettv="http://cnettv.com/mrss/"
126  xmlns:content="http://purl.org/rss/1.0/modules/content/"
127  xmlns:creativecommons="http://backend.userland.com/creativeCommonsRssModule"
128  xmlns:dc="http://purl.org/dc/elements/1.1/"
129  xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:media="http://search.yahoo.com/mrss/">
130  <channel>
131  <title>BBC iPlayer</title>
132  <link>http://www.bbc.co.uk</link>
133  <description>BBC iPlayer is our service that lets you catch up with radio and television programmes from the past week.</description>
134  <numresults>1</numresults>
135  <returned>1</returned>
136  <startindex>1</startindex>
137  <directory name="BBC iPlayer" thumbnail="/usr/local/share/mythtv/mythnetvision/icons/bbciplayer.png">
138  <directory name="BBC iPlayer - TV Highlights" thumbnail="/usr/local/share/mythtv/mythnetvision/icons/bbciplayer.png">
139  <item>
140  <title>Great Ormond Street: Pushing the Boundaries</title>
141  <author>BBC</author>
142  <pubDate>Wed, 07 Apr 2010 17:00:49 GMT</pubDate>
143  <description>A look at the work of the largest children&amp;apos;s cardiac unit in the UK.</description>
144  <link>http://localhost:8080/emml/cgi-bin/bbciplayer_embedded.py?videocode=b00s02ct</link>
145  <mrss:group xmlns:mrss="http://search.yahoo.com/mrss/">
146  <mrss:thumbnail url="http://node2.bbcimg.co.uk/iplayer/images/episode/b00s02ct_150_84.jpg"/>
147  <mrss:content duration="" height="" lang=""
148  url="http://localhost:8080/emml/cgi-bin/bbciplayer_embedded.py?videocode=b00s02ct" width=""/>
149  </mrss:group>
150  <rating>0.0</rating>
151  </item>
152 ...
153  <item>
154  <title>The Real Hustle: Series 7: On Holiday - Cutdowns: Episode 8</title>
155  <author>BBC</author>
156  <pubDate>Fri, 02 Apr 2010 07:29:46 GMT</pubDate>
157  <description>Paul relieves some unsuspecting tourists of their spending money in Oxford.</description>
158  <link>http://localhost:8080/emml/cgi-bin/bbciplayer_embedded.py?videocode=b00rw6xb</link>
159  <mrss:group xmlns:mrss="http://search.yahoo.com/mrss/">
160  <mrss:thumbnail url="http://node2.bbcimg.co.uk/iplayer/images/episode/b00rw6xb_150_84.jpg"/>
161  <mrss:content duration="" height="" lang=""
162  url="http://localhost:8080/emml/cgi-bin/bbciplayer_embedded.py?videocode=b00rw6xb" width=""/>
163  </mrss:group>
164  <rating>0.0</rating>
165  </item>
166  </directory>
167  </directory>
168  </channel>
169 </rss>
170 '''
171 __search_max_page_items__ = 20
172 __tree_max_page_items__ = 20
173 
174 import sys, os
175 
176 
177 class OutStreamEncoder(object):
178  """Wraps a stream with an encoder"""
179  def __init__(self, outstream, encoding=None):
180  self.out = outstream
181  if not encoding:
182  self.encoding = sys.getfilesystemencoding()
183  else:
184  self.encoding = encoding
185 
186  def write(self, obj):
187  """Wraps the output stream, encoding Unicode strings with the specified encoding"""
188  if isinstance(obj, unicode):
189  try:
190  self.out.write(obj.encode(self.encoding))
191  except IOError:
192  pass
193  else:
194  try:
195  self.out.write(obj)
196  except IOError:
197  pass
198 
199  def __getattr__(self, attr):
200  """Delegate everything but write to the stream"""
201  return getattr(self.out, attr)
202 sys.stdout = OutStreamEncoder(sys.stdout, 'utf8')
203 sys.stderr = OutStreamEncoder(sys.stderr, 'utf8')
204 
205 
206 # Used for debugging
207 #import nv_python_libs.common.common_api
208 try:
209  '''Import the common python class
210  '''
211  import nv_python_libs.common.common_api as common_api
212 except Exception, e:
213  sys.stderr.write('''
214 The subdirectory "nv_python_libs/common" containing the modules common_api.py and
215 common_exceptions.py (v0.1.3 or greater),
216 They should have been included with the distribution of MythNetvision
217 Error(%s)
218 ''' % e)
219  sys.exit(1)
220 if common_api.__version__ < '0.1.3':
221  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__)
222  sys.exit(1)
223 
224 # Used for debugging
225 #import nv_python_libs.bbciplayer.bbciplayer_api as target
226 try:
227  '''Import the python bbciplayer support classes
228  '''
230 except Exception, e:
231  sys.stderr.write('''
232 The subdirectory "nv_python_libs/bbciplayer" containing the modules bbciplayer_api and
233 bbciplayer_exceptions.py (v0.1.0 or greater),
234 They should have been included with the distribution of bbciplayer.py.
235 Error(%s)
236 ''' % e)
237  sys.exit(1)
238 if target.__version__ < '0.1.0':
239  sys.stderr.write("\n! Error: Your current installed bbciplayer_api.py version is (%s)\nYou must at least have version (0.1.0) or higher.\n" % target.__version__)
240  sys.exit(1)
241 
242 # Verify that the main process modules are installed and accessible
243 try:
244  import nv_python_libs.mainProcess as process
245 except Exception, e:
246  sys.stderr.write('''
247 The python script "nv_python_libs/mainProcess.py" must be present.
248 Error(%s)
249 ''' % e)
250  sys.exit(1)
251 
252 if process.__version__ < '0.2.0':
253  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__)
254  sys.exit(1)
255 
256 if __name__ == '__main__':
257  # No api key is required
258  apikey = ""
259  # Set the base processing directory that the grabber is installed
260  target.baseProcessingDir = os.path.dirname( os.path.realpath( __file__ ))
261  # Make sure the target functions have an instance of the common routines
262  target.common = common_api.Common()
263  main = process.mainProcess(target, apikey, )
264  main.grabberInfo = {}
265  main.grabberInfo['title'] = __title__
266  main.grabberInfo['mashup_title'] = __mashup_title__
267  main.grabberInfo['command'] = u'bbciplayer.py'
268  main.grabberInfo['author'] = __author__
269  main.grabberInfo['thumbnail'] = 'bbciplayer.png'
270  main.grabberInfo['type'] = ['video', ]
271  main.grabberInfo['desc'] = u"BBC iPlayer is our service that lets you catch up with radio and television programmes from the past week."
272  main.grabberInfo['version'] = __version__
273  main.grabberInfo['search'] = True
274  main.grabberInfo['tree'] = True
275  main.grabberInfo['html'] = False
276  main.grabberInfo['usage'] = __usage_examples__
277  main.grabberInfo['SmaxPage'] = __search_max_page_items__
278  main.grabberInfo['TmaxPage'] = __tree_max_page_items__
279  main.main()
def __init__(self, outstream, encoding=None)
Definition: bbciplayer.py:179
def __getattr__(self, attr)
Definition: bbciplayer.py:199