MythTV master
cinemarv_api.py
Go to the documentation of this file.
1# -*- coding: UTF-8 -*-
2
3# ----------------------
4# Name: cinemarv_api - XPath and XSLT functions for the CinemaRV.com grabber
5# Python Script
6# Author: R.D. Vaughan
7# Purpose: This python script is intended to perform a variety of utility functions
8# for the conversion of data to the MNV standard RSS output format.
9# See this link for the specifications:
10# http://www.mythtv.org/wiki/MythNetvision_Grabber_Script_Format
11#
12# License:Creative Commons GNU GPL v2
13# (http://creativecommons.org/licenses/GPL/2.0/)
14#-------------------------------------
15__title__ ="cinemarv_api - XPath and XSLT functions for the CinemaRV.com grabber"
16__author__="R.D. Vaughan"
17__purpose__='''
18This python script is intended to perform a variety of utility functions
19for the conversion of data to the MNV standard RSS output format.
20See this link for the specifications:
21http://www.mythtv.org/wiki/MythNetvision_Grabber_Script_Format
22'''
23
24__version__="v0.1.0"
25# 0.1.0 Initial development
26
27
28# Specify the class names that have XPath extention functions
29__xpathClassList__ = ['xpathFunctions', ]
30
31# Specify the XSLT extention class names. Each class is a stand lone extention function
32#__xsltExtentionList__ = ['xsltExtExample', ]
33__xsltExtentionList__ = []
34
35import os, sys, re, time, datetime, shutil, urllib.request, urllib.parse, urllib.error, string
36from copy import deepcopy
37import io
38
39class OutStreamEncoder(object):
40 """Wraps a stream with an encoder"""
41 def __init__(self, outstream, encoding=None):
42 self.out = outstream
43 if not encoding:
44 self.encoding = sys.getfilesystemencoding()
45 else:
46 self.encoding = encoding
47
48 def write(self, obj):
49 """Wraps the output stream, encoding Unicode strings with the specified encoding"""
50 if isinstance(obj, str):
51 obj = obj.encode(self.encoding)
52 try:
53 self.out.buffer.write(obj)
54 except OSError:
55 pass
56
57 def __getattr__(self, attr):
58 """Delegate everything but write to the stream"""
59 return getattr(self.out, attr)
60
61if isinstance(sys.stdout, io.TextIOWrapper):
62 sys.stdout = OutStreamEncoder(sys.stdout, 'utf8')
63 sys.stderr = OutStreamEncoder(sys.stderr, 'utf8')
64
65try:
66 from io import StringIO
67 from lxml import etree
68except Exception as e:
69 sys.stderr.write('\n! Error - Importing the "lxml" and "StringIO" python libraries failed on error(%s)\n' % e)
70 sys.exit(1)
71
72
73class xpathFunctions(object):
74 """Functions specific extending XPath
75 """
76 def __init__(self):
77 self.functList = ['cinemarvLinkGeneration', 'cinemarvIsCustomHTML', 'cinemarvCheckIfDBItem', ]
78 self.TextTail = etree.XPath("string()")
79 self.persistence = {}
80 # end __init__()
81
82
87
88 def cinemarvLinkGeneration(self, context, *args):
89 '''Generate a link for the CinemaRV.com site. A read of the item's web page is required to
90 extract the flash video id.
91 Call example: 'mnvXpath:cinemarvLinkGeneration(string(link))'
92 return the url link
93 '''
94 webURL = args[0]
95 # If this is for the download then just return what was found for the "link" element
96 if 'cinemarvLinkGeneration' in self.persistence:
97 if self.persistence['cinemarvLinkGeneration'] is not None:
98 returnValue = self.persistence['cinemarvLinkGeneration']
99 self.persistence['cinemarvLinkGeneration'] = None
100 return returnValue
101 else:
102 self.persistence['cinemarvLinkGenerationVideoID'] = etree.XPath('//object[@id="flashObj"]//param[@name="flashVars"]/@value', namespaces=common.namespaces)
103 self.persistence['cinemarvLinkGenerationParser'] = etree.HTMLParser()
104
105 try:
106 webPageElement = etree.parse(webURL, self.persistence['cinemarvLinkGenerationParser'])
107 except Exception as errmsg:
108 sys.stderr.write('!Warning: The web page URL(%s) could not be read, error(%s)\n' % (webURL, errmsg))
109 return webURL
110 if webPageElement is None:
111 self.persistence['cinemarvLinkGeneration'] = webURL
112 return webURL
113
114 tmpVideoID = self.persistence['cinemarvLinkGenerationVideoID'](webPageElement)
115 if not len(tmpVideoID):
116 self.persistence['cinemarvLinkGeneration'] = webURL
117 return webURL
118 index = tmpVideoID[0].find('&')
119 if index == -1:
120 self.persistence['cinemarvLinkGeneration'] = webURL
121 return webURL
122 videocode = tmpVideoID[0][:index].replace('videoId=', '')
123 self.persistence['cinemarvLinkGeneration'] = common.linkWebPage('dummycontext', 'cinemarv')+videocode
124 return self.persistence['cinemarvLinkGeneration']
125 # end cinemarvLinkGeneration()
126
127 def cinemarvIsCustomHTML(self, context, *args):
128 '''Check if the link is for a custom HTML
129 Example call: mnvXpath:cinemarvIsCustomHTML(('dummy'))
130 return True if the link does not starts with "http://"
131 return False if the link starts with "http://"
132 '''
133 if self.persistence['cinemarvLinkGeneration'] is None:
134 return False
135
136 if self.persistence['cinemarvLinkGeneration'].startswith('http://'):
137 return False
138 else:
139 return True
140 # end cinemarvIsCustomHTML()
141
142 def cinemarvCheckIfDBItem(self, context, *arg):
143 '''Use a unique key value pairing to find out if the 'internetcontentarticles' table already
144 has a matching item. This is done to save accessing the Internet when not required.
145 Call example: 'mnvXpath:cinemarvCheckIfDBItem(.)'
146 return True if a match was found
147 return False if a match was not found
148 '''
149 return common.checkIfDBItem('dummy', {'feedtitle': 'Movie Trailers', 'title': arg[0].replace('Trailer', '').strip(), 'author': arg[1], 'description': arg[2]})
150 # end cinemarvCheckIfDBItem()
151
152
157
158
163
164
def cinemarvLinkGeneration(self, context, *args)
Start of XPath extension functions.
Definition: cinemarv_api.py:88
static pid_list_t::iterator find(const PIDInfoMap &map, pid_list_t &list, pid_list_t::iterator begin, pid_list_t::iterator end, bool find_open)