MythTV  master
space.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 # -*- coding: UTF-8 -*-
3 # ----------------------
4 # Name: space.py
5 # Python Script
6 # Author: R.D. Vaughan
7 # Purpose:
8 # This python script is intended to perform a mashup of various Space 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/spaceMashup.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__ ="Space";
24 __mashup_title__ = "spaceMashup"
25 __author__="R.D. Vaughan"
26 __version__="v0.10"
27 # 0.10 Initial development
28 
29 __usage_examples__ ='''
30 (Option Help)
31 > ./space.py -h
32 Usage: ./space.py -hduvlST [parameters] <search text>
33 Version: v0.1.0 Author: R.D.Vaughan
34 
35 For details on the MythTV Netvision plugin see the wiki page at:
36 http://www.mythtv.org/wiki/MythNetvision
37 
38 Options:
39  -h, --help show this help message and exit
40  -d, --debug Show debugging info (URLs, raw XML ... etc, info
41  varies per grabber)
42  -u, --usage Display examples for executing the script
43  -v, --version Display grabber name and supported options
44  -l LANGUAGE, --language=LANGUAGE
45  Select data that matches the specified language fall
46  back to English if nothing found (e.g. 'es' EspaƱol,
47  'de' Deutsch ... etc). Not all sites or grabbers
48  support this option.
49  -p PAGE NUMBER, --pagenumber=PAGE NUMBER
50  Display specific page of the search results. Default
51  is page 1. Page number is ignored with the Tree View
52  option (-T).
53  -T, --treeview Display a Tree View of a sites videos
54 
55 > ./space.py -v
56 Space Mashup|T
57 
58 
59 > ./space.py -T
60 '''
61 __search_max_page_items__ = 20
62 __tree_max_page_items__ = 20
63 
64 import sys, os
65 import io
66 
67 class OutStreamEncoder(object):
68  """Wraps a stream with an encoder"""
69  def __init__(self, outstream, encoding=None):
70  self.out = outstream
71  if not encoding:
72  self.encoding = sys.getfilesystemencoding()
73  else:
74  self.encoding = encoding
75 
76  def write(self, obj):
77  """Wraps the output stream, encoding Unicode strings with the specified encoding"""
78  if isinstance(obj, str):
79  obj = obj.encode(self.encoding)
80  self.out.buffer.write(obj)
81 
82  def __getattr__(self, attr):
83  """Delegate everything but write to the stream"""
84  return getattr(self.out, attr)
85 
86 if isinstance(sys.stdout, io.TextIOWrapper):
87  sys.stdout = OutStreamEncoder(sys.stdout, 'utf8')
88  sys.stderr = OutStreamEncoder(sys.stderr, 'utf8')
89 
90 
91 # Used for debugging
92 #import nv_python_libs.common.common_api
93 
94 try:
95  '''Import the common python class
96  '''
97  import nv_python_libs.common.common_api as common_api
98 except Exception as e:
99  sys.stderr.write('''
100 The subdirectory "nv_python_libs/common" containing the modules mashups_api.py and
101 mashups_exceptions.py (v0.1.3 or greater),
102 They should have been included with the distribution of MythNetvision
103 Error(%s)
104 ''' % e)
105  sys.exit(1)
106 
107 if common_api.__version__ < '0.1.3':
108  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__)
109  sys.exit(1)
110 
111 # Used for debugging
112 #import nv_python_libs.mashups.mashups_api as target
113 try:
114  '''Import the python mashups support classes
115  '''
116  import nv_python_libs.mashups.mashups_api as target
117 except Exception as e:
118  sys.stderr.write('''
119 The subdirectory "nv_python_libs/mashups" containing the modules mashups_api and
120 mashups_exceptions.py (v0.1.0 or greater),
121 They should have been included with the distribution of space.py.
122 Error(%s)
123 ''' % e)
124  sys.exit(1)
125 if target.__version__ < '0.1.0':
126  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__)
127  sys.exit(1)
128 
129 # Verify that the main process modules are installed and accessible
130 try:
131  import nv_python_libs.mainProcess as process
132 except Exception as e:
133  sys.stderr.write('''
134 The python script "nv_python_libs/mainProcess.py" must be present.
135 Error(%s)
136 ''' % e)
137  sys.exit(1)
138 
139 if process.__version__ < '0.2.0':
140  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__)
141  sys.exit(1)
142 
143 if __name__ == '__main__':
144  # No api key is required
145  apikey = ""
146  # Set the base processing directory that the grabber is installed
147  target.baseProcessingDir = os.path.dirname( os.path.realpath( __file__ ))
148  # Make sure the target functions have an instance of the common routines
149  target.common = common_api.Common()
150  main = process.mainProcess(target, apikey, )
151  main.grabberInfo = {}
152  main.grabberInfo['enabled'] = True
153  main.grabberInfo['title'] = __title__
154  main.grabberInfo['command'] = 'space.py'
155  main.grabberInfo['mashup_title'] = __mashup_title__
156  main.grabberInfo['author'] = __author__
157  main.grabberInfo['thumbnail'] = 'space.png'
158  main.grabberInfo['type'] = ['video', ]
159  main.grabberInfo['desc'] = "Mashups combines media from multiple sources to create a new work"
160  main.grabberInfo['version'] = __version__
161  main.grabberInfo['search'] = target.common.checkIfDBItem('dummy', {'feedtitle': __title__, })
162  main.grabberInfo['tree'] = True
163  main.grabberInfo['html'] = False
164  main.grabberInfo['usage'] = __usage_examples__
165  main.grabberInfo['SmaxPage'] = __search_max_page_items__
166  main.grabberInfo['TmaxPage'] = __tree_max_page_items__
167  main.main()
space.OutStreamEncoder.write
def write(self, obj)
Definition: space.py:76
nv_python_libs.mashups.mashups_api
Definition: mashups_api.py:1
space.OutStreamEncoder
Definition: space.py:67
nv_python_libs.mainProcess
Definition: mainProcess.py:1
space.OutStreamEncoder.out
out
Definition: space.py:70
nv_python_libs.common.common_api
Definition: common_api.py:1
space.OutStreamEncoder.__getattr__
def __getattr__(self, attr)
Definition: space.py:82
space.OutStreamEncoder.encoding
encoding
Definition: space.py:72
space.OutStreamEncoder.__init__
def __init__(self, outstream, encoding=None)
Definition: space.py:69