MythTV  master
thewb.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: UTF-8 -*-
3 # ----------------------
4 # Name: thewb.py
5 # Python Script
6 # Author: R.D. Vaughan
7 # Purpose:
8 # This python script is intended to perform The WB video lookups for the MythTV Netvision plugin
9 # based on information found on the http://www.thewb.com/ 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__ ="The WB";
26 __mashup_title__ = "thewb"
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 > ./thewb.py -h
37 Usage: ./thewb.py -hduvlST [parameters] <search text>
38 Version: v0.11 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  -T, --treeview Display a Tree View of a sites videos
60 
61 > ./thewb.py -v
62 <grabber>
63  <name>The WB</name>
64  <author>R.D.Vaughan</author>
65  <thumbnail>thewb.png</thumbnail>
66  <type>video</type>
67  <description>Watch full episodes of your favorite shows on The WB.com, like Friends, The O.C., Veronica Mars, Pushing Daisies, Smallville, Buffy The Vampire Slayer, One Tree Hill and Gilmore Girls.</description>
68  <version>v0.11</version>
69  <search>true</search>
70  <tree>true</tree>
71 </grabber>
72 
73 > ./thewb.py -S "Firefly"
74 
75 > ./thewb.py -T
76 '''
77 __search_max_page_items__ = 20
78 __tree_max_page_items__ = 20
79 
80 import sys, os
81 
82 
83 class OutStreamEncoder(object):
84  """Wraps a stream with an encoder"""
85  def __init__(self, outstream, encoding=None):
86  self.out = outstream
87  if not encoding:
88  self.encoding = sys.getfilesystemencoding()
89  else:
90  self.encoding = encoding
91 
92  def write(self, obj):
93  """Wraps the output stream, encoding Unicode strings with the specified encoding"""
94  if isinstance(obj, unicode):
95  try:
96  self.out.write(obj.encode(self.encoding))
97  except IOError:
98  pass
99  else:
100  try:
101  self.out.write(obj)
102  except IOError:
103  pass
104 
105  def __getattr__(self, attr):
106  """Delegate everything but write to the stream"""
107  return getattr(self.out, attr)
108 sys.stdout = OutStreamEncoder(sys.stdout, 'utf8')
109 sys.stderr = OutStreamEncoder(sys.stderr, 'utf8')
110 
111 
112 # Used for debugging
113 #import nv_python_libs.common.common_api
114 try:
115  '''Import the common python class
116  '''
117  import nv_python_libs.common.common_api as common_api
118 except Exception, e:
119  sys.stderr.write('''
120 The subdirectory "nv_python_libs/common" containing the modules common_api.py and
121 common_exceptions.py (v0.1.3 or greater),
122 They should have been included with the distribution of MythNetvision
123 Error(%s)
124 ''' % e)
125  sys.exit(1)
126 if common_api.__version__ < '0.1.3':
127  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__)
128  sys.exit(1)
129 
130 # Used for debugging
131 #import nv_python_libs.thewb.thewb_api as target
132 try:
133  '''Import the python thewb support classes
134  '''
135  import nv_python_libs.thewb.thewb_api as target
136 except Exception, e:
137  sys.stderr.write('''
138 The subdirectory "nv_python_libs/thewb" containing the modules thewb_api and
139 thewb_exceptions.py (v0.1.0 or greater),
140 They should have been included with the distribution of thewb.py.
141 Error(%s)
142 ''' % e)
143  sys.exit(1)
144 if target.__version__ < '0.1.0':
145  sys.stderr.write("\n! Error: Your current installed thewb_api.py version is (%s)\nYou must at least have version (0.1.0) or higher.\n" % target.__version__)
146  sys.exit(1)
147 
148 # Verify that the main process modules are installed and accessible
149 try:
150  import nv_python_libs.mainProcess as process
151 except Exception, e:
152  sys.stderr.write('''
153 The python script "nv_python_libs/mainProcess.py" must be present.
154 Error(%s)
155 ''' % e)
156  sys.exit(1)
157 
158 if process.__version__ < '0.2.0':
159  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__)
160  sys.exit(1)
161 
162 if __name__ == '__main__':
163  # No api key is required
164  apikey = ""
165  # Set the base processing directory that the grabber is installed
166  target.baseProcessingDir = os.path.dirname( os.path.realpath( __file__ ))
167  # Make sure the target functions have an instance of the common routines
168  target.common = common_api.Common()
169  main = process.mainProcess(target, apikey, )
170  main.grabberInfo = {}
171  main.grabberInfo['title'] = __title__
172  main.grabberInfo['command'] = u'thewb.py'
173  main.grabberInfo['mashup_title'] = __mashup_title__
174  main.grabberInfo['author'] = __author__
175  main.grabberInfo['thumbnail'] = 'thewb.png'
176  main.grabberInfo['type'] = ['video', ]
177  main.grabberInfo['desc'] = u"Watch full episodes of your favorite shows on The WB.com, like Friends, The O.C., Veronica Mars, Pushing Daisies, Smallville, Buffy The Vampire Slayer, One Tree Hill and Gilmore Girls."
178  main.grabberInfo['version'] = __version__
179  main.grabberInfo['search'] = True
180  main.grabberInfo['tree'] = True
181  main.grabberInfo['html'] = False
182  main.grabberInfo['usage'] = __usage_examples__
183  main.grabberInfo['SmaxPage'] = __search_max_page_items__
184  main.grabberInfo['TmaxPage'] = __tree_max_page_items__
185  main.main()
def write(self, obj)
Definition: thewb.py:92
def __getattr__(self, attr)
Definition: thewb.py:105
def __init__(self, outstream, encoding=None)
Definition: thewb.py:85