MythTV  master
mnvsearch.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: UTF-8 -*-
3 # ----------------------
4 # Name: mnvsearch.py
5 # Python Script
6 # Author: R.D. Vaughan
7 # Purpose:
8 # This python script is intended to perform a data base search of MythNetvision data base tables for
9 # videos based on a command line search term.
10 # It 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 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__ ="Search all tree views";
26 __mashup_title__ = "mnvsearch"
27 __author__="R.D. Vaughan"
28 __version__="0.13"
29 # 0.1.0 Initial development
30 # 0.11 Change to support xml version information display
31 # 0.12 Added the "command" tag to the xml version information display
32 # 0.13 Converted to new common_api.py library
33 
34 __usage_examples__ ='''
35 (Option Help)
36 > ./mnvsearch.py -h
37 Usage: ./mnvsearch.py -hduvlS [parameters] <search text>
38 Version: v0.1.0 Author: R.D.Vaughan
39 
40 For details on the MythTV Netvision plugin see the wiki page at:
41 http://www.mythtv.org/wiki/MythNetvision
42 
43 Options:
44  -h, --help show this help message and exit
45  -d, --debug Show debugging info (URLs, raw XML ... etc, info
46  varies per grabber)
47  -u, --usage Display examples for executing the script
48  -v, --version Display grabber name and supported options
49  -l LANGUAGE, --language=LANGUAGE
50  Select data that matches the specified language fall
51  back to English if nothing found (e.g. 'es' EspaƱol,
52  'de' Deutsch ... etc). Not all sites or grabbers
53  support this option.
54  -p PAGE NUMBER, --pagenumber=PAGE NUMBER
55  Display specific page of the search results. Default
56  is page 1. Page number is ignored with the Tree View
57  option (-T).
58  -S, --search Search for videos
59 
60 > ./mnvsearch.py -v
61 <grabber>
62  <name>Search all tree views</name>
63  <author>R.D.Vaughan</author>
64  <thumbnail>mnvsearch.png</thumbnail>
65  <type>video</type>
66  <description>MythNetvision treeview data base search</description>
67  <version>v0.11</version>
68  <search>true</search>
69 </grabber>
70 
71 > ./mnvsearch.py -S "Doctor Who"
72 <?xml version="1.0" encoding="UTF-8"?>
73 <rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:cnettv="http://cnettv.com/mrss/" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:media="http://search.yahoo.com/mrss/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:amp="http://www.adobe.com/amp/1.0" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
74 <channel>
75  <title>Search all tree views</title>
76  <link>http://www.mythtv.org/wiki/MythNetvision</link>
77  <description>MythNetvision treeview data base search</description>
78  <numresults>21</numresults>
79  <returned>20</returned>
80  <startindex>20</startindex>
81 <item>
82  <title>Doctor Who - Doctor Who and The Brain of Morbius - Episode 8</title>
83  <author>BBC</author>
84  <pubDate>Sat, 01 May 2010 15:04:02 GMT</pubDate>
85  <description>The Doctor and Sarah Jane confront the Morbius monster and seek help from the Sisterhood.</description>
86  <link>file:///usr/local/share/mythtv/mythnetvision/scripts/nv_python_libs/configs/HTML/bbciplayer.html?videocode=b00s5ztx</link>
87  <media:group>
88  <media:thumbnail url="http://node1.bbcimg.co.uk/iplayer/images/episode/b00s5ztx_120_68.jpg"/>
89  <media:content url="file:///usr/local/share/mythtv/mythnetvision/scripts/nv_python_libs/configs/HTML/bbciplayer.html?videocode=b00s5ztx" length="" duration="" width="" height="" lang=""/>
90  </media:group>
91  <rating>0.0</rating>
92 </item>
93 ...
94 <item>
95  <title>Every Doctor Who Story 1963-2008 - by Babelcolour</title>
96  <author>BabelColour</author>
97  <pubDate>Mon, 07 Jul 2008 14:45:12 GMT</pubDate>
98  <description>To celebrate the 45th Anniversary of the series, here is every Who story from 1963 to 2008, with the spin-off shows and bbci internet productions &amp; the Children In Need specials, but doesn't include any of the spoofs, comedy sketches or other charity skits not made by the official Who Production Team. Edit: It was made &amp; uploaded before the BBC Proms Special 'Music Of The Spheres'. That's why it isn't included! The fabulous music mix (called 'Whorythmics') was created by jex</description>
99  <link>http://www.youtube.com/v/lCZhlEdGIm0?f=videos&amp;app=youtube_gdata&amp;autoplay=1</link>
100  <media:group>
101  <media:thumbnail url="http://i.ytimg.com/vi/lCZhlEdGIm0/hqdefault.jpg"/>
102  <media:content url="http://www.youtube.com/v/lCZhlEdGIm0?f=videos&amp;app=youtube_gdata&amp;autoplay=1" length="" duration="" width="" height="" lang=""/>
103  </media:group>
104  <rating>4.957553</rating>
105 </item></channel></rss>
106 '''
107 __search_max_page_items__ = 20
108 __tree_max_page_items__ = 20
109 
110 import sys, os
111 
112 
113 class OutStreamEncoder(object):
114  """Wraps a stream with an encoder"""
115  def __init__(self, outstream, encoding=None):
116  self.out = outstream
117  if not encoding:
118  self.encoding = sys.getfilesystemencoding()
119  else:
120  self.encoding = encoding
121 
122  def write(self, obj):
123  """Wraps the output stream, encoding Unicode strings with the specified encoding"""
124  if isinstance(obj, unicode):
125  try:
126  self.out.write(obj.encode(self.encoding))
127  except IOError:
128  pass
129  else:
130  try:
131  self.out.write(obj)
132  except IOError:
133  pass
134 
135  def __getattr__(self, attr):
136  """Delegate everything but write to the stream"""
137  return getattr(self.out, attr)
138 sys.stdout = OutStreamEncoder(sys.stdout, 'utf8')
139 sys.stderr = OutStreamEncoder(sys.stderr, 'utf8')
140 
141 
142 # Used for debugging
143 #import nv_python_libs.common.common_api
144 try:
145  '''Import the common python class
146  '''
147  import nv_python_libs.common.common_api as common_api
148 except Exception, e:
149  sys.stderr.write('''
150 The subdirectory "nv_python_libs/common" containing the modules common_api.py and
151 common_exceptions.py (v0.1.3 or greater),
152 They should have been included with the distribution of MythNetvision
153 Error(%s)
154 ''' % e)
155  sys.exit(1)
156 if common_api.__version__ < '0.1.3':
157  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__)
158  sys.exit(1)
159 
160 # Used for debugging
161 #import nv_python_libs.mnvsearch.mnvsearch_api as target
162 try:
163  '''Import the python mnvsearch support classes
164  '''
166 except Exception, e:
167  sys.stderr.write('''
168 The subdirectory "nv_python_libs/mnvsearch" containing the modules mnvsearch_api and
169 mnvsearch_exceptions.py (v0.1.0 or greater),
170 They should have been included with the distribution of mnvsearch.py.
171 Error(%s)
172 ''' % e)
173  sys.exit(1)
174 if target.__version__ < '0.1.0':
175  sys.stderr.write("\n! Error: Your current installed mnvsearch_api.py version is (%s)\nYou must at least have version (0.1.0) or higher.\n" % target.__version__)
176  sys.exit(1)
177 
178 # Verify that the main process modules are installed and accessible
179 try:
180  import nv_python_libs.mainProcess as process
181 except Exception, e:
182  sys.stderr.write('''
183 The python script "nv_python_libs/mainProcess.py" must be present.
184 Error(%s)
185 ''' % e)
186  sys.exit(1)
187 
188 if process.__version__ < '0.2.0':
189  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__)
190  sys.exit(1)
191 
192 if __name__ == '__main__':
193  # No api key is required
194  apikey = ""
195  # Set the base processing directory that the grabber is installed
196  target.baseProcessingDir = os.path.dirname( os.path.realpath( __file__ ))
197  # Make sure the target functions have an instance of the common routines
198  target.common = common_api.Common()
199  main = process.mainProcess(target, apikey, )
200  main.grabberInfo = {}
201  main.grabberInfo['title'] = __title__
202  main.grabberInfo['command'] = u'mnvsearch.py'
203  main.grabberInfo['mashup_title'] = __mashup_title__
204  main.grabberInfo['author'] = __author__
205  main.grabberInfo['thumbnail'] = 'mnvsearch.png'
206  main.grabberInfo['type'] = ['video', ]
207  main.grabberInfo['desc'] = u"MythTV Online Content database search."
208  main.grabberInfo['version'] = __version__
209  main.grabberInfo['search'] = True
210  main.grabberInfo['tree'] = False
211  main.grabberInfo['html'] = False
212  main.grabberInfo['usage'] = __usage_examples__
213  main.grabberInfo['SmaxPage'] = __search_max_page_items__
214  main.grabberInfo['TmaxPage'] = __tree_max_page_items__
215  main.main()
def __getattr__(self, attr)
Definition: mnvsearch.py:135
def write(self, obj)
Definition: mnvsearch.py:122
def __init__(self, outstream, encoding=None)
Definition: mnvsearch.py:115