MythTV master
traileraddicts_api.py
Go to the documentation of this file.
1# -*- coding: UTF-8 -*-
2
3# ----------------------
4# Name: traileraddicts_api - XPath and XSLT functions for the TrailerAddicts.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__ ="traileraddicts_api - XPath and XSLT functions for the TrailerAddicts.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 = ['traileraddictsLinkGenerationMovie', 'traileraddictsLinkGenerationClip', 'traileraddictsCheckIfDBItem']
78 self.TextTail = etree.XPath("string()")
79 self.persistence = {}
80 # end __init__()
81
82
87
88 def traileraddictsLinkGenerationMovie(self, context, *args):
89 '''Generate a link for the TrailerAddicts.com site.
90 Call example: 'mnvXpath:traileraddictsLinkGenerationMovie(position(), link)'
91 return the url link
92 '''
93 webURL = args[1].strip()
94
95 # If this is for the download element then just return what was found for the "link" element
96 if 'traileraddictsLinkGenerationMovie' in self.persistence:
97 if args[0] == self.persistence['traileraddictsLinkGenerationMovie']['position']:
98 return self.persistence['traileraddictsLinkGenerationMovie']['link']
99 else:
100 self.persistence['traileraddictsLinkGenerationMovie'] = {}
101 self.persistence['traileraddictsLinkGenerationMovie']['embedRSS'] = etree.parse('http://www.traileraddict.com/embedrss', common.parsers['xml'].copy())
102 self.persistence['traileraddictsLinkGenerationMovie']['matchlink'] = etree.XPath('//link[string()=$link]/..', namespaces=common.namespaces)
103 self.persistence['traileraddictsLinkGenerationMovie']['description'] = etree.XPath('normalize-space(description)', namespaces=common.namespaces)
104 self.persistence['traileraddictsLinkGenerationMovie']['embedded'] = etree.XPath('//embed/@src', namespaces=common.namespaces)
105
106 self.persistence['traileraddictsLinkGenerationMovie']['position'] = args[0]
107
108 matchLink = self.persistence['traileraddictsLinkGenerationMovie']['matchlink'](self.persistence['traileraddictsLinkGenerationMovie']['embedRSS'], link=webURL)[0]
109 self.persistence['traileraddictsLinkGenerationMovie']['link'] = self.persistence['traileraddictsLinkGenerationMovie']['embedded'](common.getHtmlData('dummy',(self.persistence['traileraddictsLinkGenerationMovie']['description'](matchLink))))[0]
110
111 return self.persistence['traileraddictsLinkGenerationMovie']['link']
112 # end traileraddictsLinkGenerationMovie()
113
114
115 def traileraddictsLinkGenerationClip(self, context, *args):
116 '''Generate a link for the TrailerAddicts.com site.
117 Call example: 'mnvXpath:traileraddictsLinkGenerationClip(position(), link)'
118 return the url link
119 '''
120 webURL = args[1].strip()
121 # If this is for the download element then just return what was found for the "link" element
122 if 'traileraddictsLinkGenerationClip' in self.persistence:
123 if args[0] == self.persistence['traileraddictsLinkGenerationClip']['position']:
124 return self.persistence['traileraddictsLinkGenerationClip']['link']
125 else:
126 self.persistence['traileraddictsLinkGenerationClip'] = {}
127 self.persistence['traileraddictsLinkGenerationClip']['embedded'] = etree.XPath('//embed[@allowfullscreen="true"]/@src', namespaces=common.namespaces)
128
129 self.persistence['traileraddictsLinkGenerationClip']['position'] = args[0]
130
131 tmpHTML = etree.parse(webURL, etree.HTMLParser())
132 self.persistence['traileraddictsLinkGenerationClip']['link'] = self.persistence['traileraddictsLinkGenerationClip']['embedded'](tmpHTML)[0]
133 return self.persistence['traileraddictsLinkGenerationClip']['link']
134 # end traileraddictsLinkGenerationClip()
135
136 def traileraddictsCheckIfDBItem(self, context, *arg):
137 '''Use a unique key value pairing to find out if the 'internetcontentarticles' table already
138 has a matching item. This is done to save accessing the Internet when not required.
139 Call example: 'mnvXpath:traileraddictsCheckIfDBItem(.)'
140 return True if a match was found
141 return False if a match was not found
142 '''
143 return common.checkIfDBItem('dummy', {'feedtitle': 'Movie Trailers', 'title': arg[0], 'author': arg[1], 'description': arg[2]})
144 # end traileraddictsCheckIfDBItem()
145
146
151
152
157
158
159
def traileraddictsLinkGenerationMovie(self, context, *args)
Start of XPath extension functions.
MBASE_PUBLIC long long copy(QFile &dst, QFile &src, uint block_size=0)
Copies src file to dst file.