15__title__ =
"youtubeXSL_api - XPath and XSLT functions for the mashup grabbers"
16__author__=
"R.D. Vaughan"
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
29__xpathClassList__ = [
'xpathFunctions', ]
33__xsltExtentionList__ = []
35import os, sys, re, time, datetime, shutil,
urllib.request, urllib.parse, urllib.error, string
36from copy
import deepcopy
41 """Wraps a stream with an encoder"""
50 """Wraps the output stream, encoding Unicode strings with the specified encoding"""
51 if isinstance(obj, str):
54 self.
out.buffer.write(obj)
59 """Delegate everything but write to the stream"""
60 return getattr(self.
out, attr)
62if isinstance(sys.stdout, io.TextIOWrapper):
67 from io
import StringIO
68 from lxml
import etree
70 sys.stderr.write(
'\n! Error - Importing the "lxml" and "StringIO" python libraries failed on error(%s)\n' % e)
75 """Functions specific extending XPath
78 self.
functList = [
'youtubeTrailerFilter',
'youtubePaging', ]
81 re.compile(
'''^.+?trailer\\ (?P<trailerNum>[0-9]+).*$''', re.UNICODE),
83 re.compile(
'''^.+?trailer\\ \\#(?P<trailerNum>[0-9]+).*$''', re.UNICODE),
94 '''Generate a list of entry elements that are relevant to the requested search term. Basically
95 remove duplicate and non-relevant search results
and order them to provide the best results
97 Also set the paging variables.
98 Call example:
'mnvXpath:youtubeTrailerFilter(//atm:entry)'
99 return the list of relevant
"entry" elements
101 searchTerm = common.removePunc('dummy', common.searchterm.lower())
102 titleFilter = etree.XPath(
'.//atm:title', namespaces=common.namespaces)
105 if searchTerm.startswith(
'the '):
106 searchTerm = searchTerm[4:].strip()
109 for entry
in args[0]:
110 titleDict[titleFilter(entry)[0].text] = entry
114 for key
in list(titleDict.keys()):
115 title = common.removePunc(
'dummy', key.lower())
116 if title.startswith(
'the '):
117 title = title[4:].strip()
118 if searchTerm.find(
'new ') == -1:
119 title = title.replace(
'new ',
'')
120 if searchTerm.find(
'official ') == -1:
121 title = title.replace(
'official ',
'')
122 if title.find(searchTerm) != -1:
125 if searchTerm.find(
'game ') == -1:
126 if title.find(
'game') != -1:
128 if title.find(
'hd') != -1
or title.find(
'1080p') != -1
or title.find(
'720p') != -1:
130 if title.startswith(searchTerm):
133 match = regexPattern.match(title)
136 trailerNum = match.groups()
137 if int(trailerNum[0]) < 20:
138 addOns+=
'Trailer #%s' % trailerNum[0]
139 title = title.replace((
'trailer %s' % trailerNum[0]),
'')
144 if title.find(
'trailer') != -1:
146 if HD
and not addOns.startswith(
'ZZ-Game'):
151 for text
in [
'hd',
'trailer',
'game',
'1080p',
'720p']:
152 title = title.replace(text,
'').replace(
' ',
' ').strip()
153 filteredDict[(
'%s %s' % (addOns, title)).strip()] = titleDict[key]
157 sortedList = sorted(filteredDict.keys())
158 for index
in range(len(sortedList)):
160 filtered2Dict[sortedList[index]] = deepcopy(filteredDict[sortedList[index]])
162 if sortedList[index] != sortedList[index-1]:
163 filtered2Dict[sortedList[index]] = deepcopy(filteredDict[sortedList[index]])
167 sortedList = sorted(filtered2Dict.keys())
168 for index
in range(len(sortedList)):
169 titleFilter(filtered2Dict[sortedList[index]])[0].text =
'%02d. %s' % (index+1, titleFilter(filtered2Dict[sortedList[index]])[0].text)
170 finalElements.append(filtered2Dict[sortedList[index]])
173 common.numresults = str(len(finalElements))
174 common.returned = common.numresults
175 common.startindex = common.numresults
181 '''Generate a page value specific to the mashup search for YouTube searches
182 Call example: 'mnvXpath:youtubePaging('dummy
')'
183 The page value
is some times a page
184 return the page value that will be used
in the search
as a string
186 return str((int(common.pagenumber) -1) * common.page_limit + 1)
def __init__(self, outstream, encoding=None)
def __getattr__(self, attr)
def youtubeTrailerFilter(self, context, *args)
Start of XPath extension functions.
def youtubePaging(self, context, args)