MythTV  master
nature.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: UTF-8 -*-
3 # ----------------------
4 # Name: nature.py
5 # Python Script
6 # Author: R.D. Vaughan
7 # Purpose:
8 # This python script is intended to perform a mashup of various Nature 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/natureMashup.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__ ="Nature";
24 __mashup_title__ = "natureMashup"
25 __author__="R.D. Vaughan"
26 __version__="0.12"
27 # 0.10 Initial development
28 # 0.11 Added a search of all the "Nature Mashup" items in the "internetcontentarticles" table
29 # 0.12 Toggle the search option off if there are NO "Nature" items in the "internetcontentarticles"
30 # table
31 
32 __usage_examples__ ='''
33 (Option Help)
34 > ./nature.py -h
35 Usage: ./nature.py -hduvlST [parameters] <search text>
36 Version: v0.1.0 Author: R.D.Vaughan
37 
38 For details on the MythTV Netvision plugin see the wiki page at:
39 http://www.mythtv.org/wiki/MythNetvision
40 
41 Options:
42  -h, --help show this help message and exit
43  -d, --debug Show debugging info (URLs, raw XML ... etc, info
44  varies per grabber)
45  -u, --usage Display examples for executing the script
46  -v, --version Display grabber name and supported options
47  -l LANGUAGE, --language=LANGUAGE
48  Select data that matches the specified language fall
49  back to English if nothing found (e.g. 'es' EspaƱol,
50  'de' Deutsch ... etc). Not all sites or grabbers
51  support this option.
52  -p PAGE NUMBER, --pagenumber=PAGE NUMBER
53  Display specific page of the search results. Default
54  is page 1. Page number is ignored with the Tree View
55  option (-T).
56  -T, --treeview Display a Tree View of a sites videos
57 
58 > ./nature.py -v
59 <grabber>
60  <name>Nature</name>
61  <author>R.D. Vaughan</author>
62  <thumbnail>nature.png</thumbnail>
63  <command>nature.py</command>
64  <type>video</type>
65  <description>Mashups combines media from multiple sources to create a new work</description>
66  <version>0.XX</version>
67  <search>true</search>
68  <tree>true</tree>
69 </grabber>
70 
71 Search:
72 > ./nature.py -S "Lions"
73 
74 Treeview:
75 > ./nature.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 
131 # Used for debugging
132 #import nv_python_libs.mashups.mashups_api as target
133 try:
134  '''Import the python mashups support classes
135  '''
136  import nv_python_libs.mashups.mashups_api as target
137 except Exception, e:
138  sys.stderr.write('''
139 The subdirectory "nv_python_libs/mashups" containing the modules mashups_api and
140 mashups_exceptions.py (v0.1.0 or greater),
141 They should have been included with the distribution of nature.py.
142 Error(%s)
143 ''' % e)
144  sys.exit(1)
145 if target.__version__ < '0.1.0':
146  sys.stderr.write("\n! Error: Your current installed mashups_api.py version is (%s)\nYou must at least have version (0.1.0) or higher.\n" % target.__version__)
147  sys.exit(1)
148 
149 # Verify that the main process modules are installed and accessible
150 try:
151  import nv_python_libs.mainProcess as process
152 except Exception, e:
153  sys.stderr.write('''
154 The python script "nv_python_libs/mainProcess.py" must be present.
155 Error(%s)
156 ''' % e)
157  sys.exit(1)
158 
159 if process.__version__ < '0.2.0':
160  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__)
161  sys.exit(1)
162 
163 if __name__ == '__main__':
164  # No api key is required
165  apikey = ""
166  # Set the base processing directory that the grabber is installed
167  target.baseProcessingDir = os.path.dirname( os.path.realpath( __file__ ))
168  # Make sure the target functions have an instance of the common routines
169  target.common = common_api.Common()
170  main = process.mainProcess(target, apikey, )
171  main.grabberInfo = {}
172  main.grabberInfo['title'] = __title__
173  main.grabberInfo['command'] = u'nature.py'
174  main.grabberInfo['mashup_title'] = __mashup_title__
175  main.grabberInfo['author'] = __author__
176  main.grabberInfo['thumbnail'] = 'nature.png'
177  main.grabberInfo['type'] = ['video', ]
178  main.grabberInfo['desc'] = u"Mashups combines media from multiple sources to create a new work"
179  main.grabberInfo['version'] = __version__
180  # Check if there are any Nature items in the DB to to actually search
181  main.grabberInfo['search'] = target.common.checkIfDBItem('dummy', {'feedtitle': __title__, })
182  main.grabberInfo['tree'] = True
183  main.grabberInfo['html'] = False
184  main.grabberInfo['usage'] = __usage_examples__
185  main.grabberInfo['SmaxPage'] = __search_max_page_items__
186  main.grabberInfo['TmaxPage'] = __tree_max_page_items__
187  main.main()
def __init__(self, outstream, encoding=None)
Definition: nature.py:85
def write(self, obj)
Definition: nature.py:92
def __getattr__(self, attr)
Definition: nature.py:105