MythTV  master
nature.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
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 import io
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, str):
95  obj = obj.encode(self.encoding)
96  self.out.buffer.write(obj)
97 
98  def __getattr__(self, attr):
99  """Delegate everything but write to the stream"""
100  return getattr(self.out, attr)
101 
102 if isinstance(sys.stdout, io.TextIOWrapper):
103  sys.stdout = OutStreamEncoder(sys.stdout, 'utf8')
104  sys.stderr = OutStreamEncoder(sys.stderr, 'utf8')
105 
106 
107 # Used for debugging
108 #import nv_python_libs.common.common_api
109 try:
110  '''Import the common python class
111  '''
112  import nv_python_libs.common.common_api as common_api
113 except Exception as e:
114  sys.stderr.write('''
115 The subdirectory "nv_python_libs/common" containing the modules common_api.py and
116 common_exceptions.py (v0.1.3 or greater),
117 They should have been included with the distribution of MythNetvision
118 Error(%s)
119 ''' % e)
120  sys.exit(1)
121 if common_api.__version__ < '0.1.3':
122  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__)
123  sys.exit(1)
124 
125 
126 # Used for debugging
127 #import nv_python_libs.mashups.mashups_api as target
128 try:
129  '''Import the python mashups support classes
130  '''
131  import nv_python_libs.mashups.mashups_api as target
132 except Exception as e:
133  sys.stderr.write('''
134 The subdirectory "nv_python_libs/mashups" containing the modules mashups_api and
135 mashups_exceptions.py (v0.1.0 or greater),
136 They should have been included with the distribution of nature.py.
137 Error(%s)
138 ''' % e)
139  sys.exit(1)
140 if target.__version__ < '0.1.0':
141  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__)
142  sys.exit(1)
143 
144 # Verify that the main process modules are installed and accessible
145 try:
146  import nv_python_libs.mainProcess as process
147 except Exception as e:
148  sys.stderr.write('''
149 The python script "nv_python_libs/mainProcess.py" must be present.
150 Error(%s)
151 ''' % e)
152  sys.exit(1)
153 
154 if process.__version__ < '0.2.0':
155  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__)
156  sys.exit(1)
157 
158 if __name__ == '__main__':
159  # No api key is required
160  apikey = ""
161  # Set the base processing directory that the grabber is installed
162  target.baseProcessingDir = os.path.dirname( os.path.realpath( __file__ ))
163  # Make sure the target functions have an instance of the common routines
164  target.common = common_api.Common()
165  main = process.mainProcess(target, apikey, )
166  main.grabberInfo = {}
167  main.grabberInfo['enabled'] = True
168  main.grabberInfo['title'] = __title__
169  main.grabberInfo['command'] = 'nature.py'
170  main.grabberInfo['mashup_title'] = __mashup_title__
171  main.grabberInfo['author'] = __author__
172  main.grabberInfo['thumbnail'] = 'nature.png'
173  main.grabberInfo['type'] = ['video', ]
174  main.grabberInfo['desc'] = "Mashups combines media from multiple sources to create a new work"
175  main.grabberInfo['version'] = __version__
176  # Check if there are any Nature items in the DB to to actually search
177  main.grabberInfo['search'] = target.common.checkIfDBItem('dummy', {'feedtitle': __title__, })
178  main.grabberInfo['tree'] = True
179  main.grabberInfo['html'] = False
180  main.grabberInfo['usage'] = __usage_examples__
181  main.grabberInfo['SmaxPage'] = __search_max_page_items__
182  main.grabberInfo['TmaxPage'] = __tree_max_page_items__
183  main.main()
nature.OutStreamEncoder.write
def write(self, obj)
Definition: nature.py:92
nature.OutStreamEncoder
Definition: nature.py:83
nv_python_libs.mashups.mashups_api
Definition: mashups_api.py:1
nature.OutStreamEncoder.__getattr__
def __getattr__(self, attr)
Definition: nature.py:98
nv_python_libs.mainProcess
Definition: mainProcess.py:1
nv_python_libs.common.common_api
Definition: common_api.py:1
nature.OutStreamEncoder.out
out
Definition: nature.py:86
nature.OutStreamEncoder.encoding
encoding
Definition: nature.py:88
nature.OutStreamEncoder.__init__
def __init__(self, outstream, encoding=None)
Definition: nature.py:85