MythTV  master
mtv.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: UTF-8 -*-
3 # ----------------------
4 # Name: mtv.py
5 # Python Script
6 # Author: R.D. Vaughan
7 # Purpose:
8 # This python script is intended to perform MTV video lookups for the MythTV Netvision plugin
9 # based on information found on the http://www.mtv.com/ website. It
10 # follows the MythTV Netvision grabber standards.
11 # This script uses the python module mtv_api.py which should be included
12 # with this script.
13 # The mtv.py module uses the full access API published by
14 # http://www.mtv.com/ see: http://developer.mtvnservices.com/docs
15 #
16 # Command example:
17 # See help (-u and -h) options
18 #
19 # Design:
20 # 1) Import the specific target site API library.
21 # 2) Set the title for the scrips and the API optional key for the target video site
22 # 3) Call the common processing routine
23 #
24 #
25 # License:Creative Commons GNU GPL v2
26 # (http://creativecommons.org/licenses/GPL/2.0/)
27 #-------------------------------------
28 __title__ ="MTV";
29 __author__="R.D. Vaughan"
30 __version__="0.23"
31 # 0.1.0 Initial development
32 # 0.1.1 Added Tree View processing
33 # 0.1.2 Documentation review
34 # 0.2.0 Public release
35 # 0.2.1 Improve error message display when there is an abort condition
36 # 0.22 Change to support xml version information display
37 # 0.23 Added the "command" tag to the xml version information display
38 
39 __usage_examples__ ='''
40 (Option Help)
41 > ./mtv.py -h
42 Usage: ./mtv.py -hduvlST [parameters] <search text>
43 Version: v0.2.0 Author: R.D.Vaughan
44 
45 For details on the MythTV Netvision plugin see the wiki page at:
46 http://www.mythtv.org/wiki/MythNetvision
47 
48 Options:
49  -h, --help show this help message and exit
50  -d, --debug Show debugging info (URLs, raw XML ... etc, info
51  varies per grabber)
52  -u, --usage Display examples for executing the script
53  -v, --version Display grabber name and supported options
54  -l LANGUAGE, --language=LANGUAGE
55  Select data that matches the specified language fall
56  back to English if nothing found (e.g. 'es' EspaƱol,
57  'de' Deutsch ... etc). Not all sites or grabbers
58  support this option.
59  -p PAGE NUMBER, --pagenumber=PAGE NUMBER
60  Display specific page of the search results. Default
61  is page 1. Page number is ignored with the Tree View
62  option (-T).
63  -S, --search Search for videos
64  -T, --treeview Display a Tree View of a sites videos
65 
66 
67 (Search MTV for videos matching search words)
68 > ./mtv.py -S "Sleeping" -p 2
69 <?xml version="1.0" encoding="UTF-8"?>
70 <rss version="2.0"
71 xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
72 xmlns:content="http://purl.org/rss/1.0/modules/content/"
73 xmlns:cnettv="http://cnettv.com/mrss/"
74 xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
75 xmlns:media="http://search.yahoo.com/mrss/"
76 xmlns:atom="http://www.w3.org/2005/Atom"
77 xmlns:amp="http://www.adobe.com/amp/1.0"
78 xmlns:dc="http://purl.org/dc/elements/1.1/">
79  <channel>
80  <title>MTV</title>
81  <link>http://www.mtv.com</link>
82  <description>Visit MTV (Music Television) for TV shows, music videos, celebrity photos, news.</description>
83  <numresults>41</numresults>
84  <returned>20</returned>
85  <startindex>40</startindex>
86  <item>
87  <title>While You Were Sleeping</title>
88  <author>Elvis Perkins</author>
89  <pubDate>Tue, 20 Feb 2007 00:00:00 GMT</pubDate>
90  <description>Elvis Perkins - While You Were Sleeping - XL Recordings</description>
91  <link>http://media.mtvnservices.com/mgid:uma:video:api.mtvnservices.com:170754</link>
92  <media:group>
93  <media:thumbnail url='http://www.mtv.com/shared/promoimages/bands/p/perkins_elvis/while_you_were_sleeping/281x211.jpg'/>
94  <media:content url='http://media.mtvnservices.com/mgid:uma:video:api.mtvnservices.com:170754' duration='229' width='' height='' lang=''/>
95  </media:group>
96  <rating></rating>
97  </item>
98 ...
99  <item>
100  <title>She is Love</title>
101  <author>Parachute</author>
102  <pubDate>Mon, 16 Mar 2009 00:00:00 GMT</pubDate>
103  <description>Parachute - She is Love - Mercury</description>
104  <link>http://media.mtvnservices.com/mgid:uma:video:api.mtvnservices.com:353524</link>
105  <media:group>
106  <media:thumbnail url='http://www.mtv.com/shared/promoimages/bands/p/parachute/she_is_love/281x211.jpg'/>
107  <media:content url='http://media.mtvnservices.com/mgid:uma:video:api.mtvnservices.com:353524' duration='226.76' width='' height='' lang=''/>
108  </media:group>
109  <rating></rating>
110  </item>
111  </channel>
112 </rss>
113 
114 
115 (Option Tree view)
116 > ./mtv.py -T
117 <?xml version="1.0" encoding="UTF-8"?>
118 <rss version="2.0"
119 xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
120 xmlns:content="http://purl.org/rss/1.0/modules/content/"
121 xmlns:cnettv="http://cnettv.com/mrss/"
122 xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
123 xmlns:media="http://search.yahoo.com/mrss/"
124 xmlns:atom="http://www.w3.org/2005/Atom"
125 xmlns:amp="http://www.adobe.com/amp/1.0"
126 xmlns:dc="http://purl.org/dc/elements/1.1/">
127  <channel>
128  <title>MTV</title>
129  <link>http://www.mtv.com</link>
130  <description>Visit MTV (Music Television) for TV shows, music videos, celebrity photos, news.</description>
131  <numresults>5050</numresults>
132  <returned>20</returned>
133  <startindex>20</startindex>
134  <directory name="New over the last 3 months" thumbnail="/usr/local/share/mythtv/mythnetvision/icons/directories/topics/recent.png">
135  <directory name="Rock" thumbnail="/usr/local/share/mythtv/mythnetvision/icons/directories/music_genres/rock.png">
136  <item>
137  <title>Useless</title>
138  <author>Tiny Animals</author>
139  <pubDate>Thu, 17 Dec 2009 00:00:00 GMT</pubDate>
140  <description></description>
141  <link>http://media.mtvnservices.com/mgid:uma:video:api.mtvnservices.com:444694</link>
142  <media:group>
143  <media:thumbnail url='http://www.mtv.com/shared/promoimages/bands/t/tiny_animals/useless/281x211.jpg'/>
144  <media:content url='http://media.mtvnservices.com/mgid:uma:video:api.mtvnservices.com:444694' duration='212.88' width='' height='' lang='en'/>
145  </media:group>
146  <rating></rating>
147  </item>
148 ...
149  <item>
150  <title>Our Velocity</title>
151  <author>Maximo Park</author>
152  <pubDate>Mon, 02 Apr 2007 00:00:00 GMT</pubDate>
153  <description></description>
154  <link>http://media.mtvnservices.com/mgid:uma:video:api.mtvnservices.com:149873</link>
155  <media:group>
156  <media:thumbnail url='http://www.mtv.com/shared/promoimages/bands/m/maximo_park/our_velocity/281x211.jpg'/>
157  <media:content url='http://media.mtvnservices.com/mgid:uma:video:api.mtvnservices.com:149873' duration='218' width='' height='' lang='en'/>
158  </media:group>
159  <rating></rating>
160  </item>
161  </directory>
162  </directory>
163  </channel>
164 </rss>
165 '''
166 __search_max_page_items__ = 10
167 __tree_max_page_items__ = 20
168 
169 import sys, os
170 
171 class OutStreamEncoder(object):
172  """Wraps a stream with an encoder"""
173  def __init__(self, outstream, encoding=None):
174  self.out = outstream
175  if not encoding:
176  self.encoding = sys.getfilesystemencoding()
177  else:
178  self.encoding = encoding
179 
180  def write(self, obj):
181  """Wraps the output stream, encoding Unicode strings with the specified encoding"""
182  if isinstance(obj, unicode):
183  try:
184  self.out.write(obj.encode(self.encoding))
185  except IOError:
186  pass
187  else:
188  try:
189  self.out.write(obj)
190  except IOError:
191  pass
192 
193  def __getattr__(self, attr):
194  """Delegate everything but write to the stream"""
195  return getattr(self.out, attr)
196 sys.stdout = OutStreamEncoder(sys.stdout, 'utf8')
197 sys.stderr = OutStreamEncoder(sys.stderr, 'utf8')
198 
199 
200 # Used for debugging
201 #import nv_python_libs.mtv.mtv_api as target
202 
203 # Verify that the tmdb_api modules are installed and accessible
204 try:
205  import nv_python_libs.mtv.mtv_api as target
206 except Exception, e:
207  sys.stderr.write('''
208 The subdirectory "nv_python_libs/mtv" containing the modules mtv_api.py (v0.2.0 or greater),
209 They should have been included with the distribution of mtv.py.
210 Error(%s)
211 ''' % e)
212  sys.exit(1)
213 
214 if target.__version__ < '0.2.0':
215  sys.stderr.write("\n! Error: Your current installed mtv_api.py version is (%s)\nYou must at least have version (0.2.0) or higher.\n" % target.__version__)
216  sys.exit(1)
217 
218 
219 # Verify that the common process modules are installed and accessible
220 try:
221  import nv_python_libs.mainProcess as process
222 except Exception, e:
223  sys.stderr.write('''
224 The python script "nv_python_libs/mainProcess.py" must be present.
225 Error(%s)
226 ''' % e)
227  sys.exit(1)
228 
229 if process.__version__ < '0.2.0':
230  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__)
231  sys.exit(1)
232 
233 if __name__ == '__main__':
234  # No api key is required
235  apikey = ""
236  main = process.mainProcess(target, apikey, )
237  main.grabberInfo = {}
238  main.grabberInfo['title'] = __title__
239  main.grabberInfo['command'] = u'mtv.py'
240  main.grabberInfo['author'] = __author__
241  main.grabberInfo['thumbnail'] = 'mtv.png'
242  main.grabberInfo['type'] = ['video']
243  main.grabberInfo['desc'] = u"Visit MTV (Music Television) for TV shows, music videos, celebrity photos, and news."
244  main.grabberInfo['version'] = __version__
245  main.grabberInfo['search'] = True
246  main.grabberInfo['tree'] = True
247  main.grabberInfo['html'] = False
248  main.grabberInfo['usage'] = __usage_examples__
249  main.grabberInfo['SmaxPage'] = __search_max_page_items__
250  main.grabberInfo['TmaxPage'] = __tree_max_page_items__
251  main.main()
def write(self, obj)
Definition: mtv.py:180
def __init__(self, outstream, encoding=None)
Definition: mtv.py:173
def __getattr__(self, attr)
Definition: mtv.py:193