MythTV master
chrisPirillo_api.py
Go to the documentation of this file.
1# -*- coding: UTF-8 -*-
2
3# ----------------------
4# Name: chrisPirillo_api - XPath and XSLT functions for the chris.pirillo.com RSS/HTML items
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__ ="chrisPirillo_api - XPath and XSLT functions for the chris.pirillo.com RSS/HTML"
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# Specify the class names that have XPath extention functions
28__xpathClassList__ = ['xpathFunctions', ]
29
30# Specify the XSLT extention class names. Each class is a stand lone extention function
31#__xsltExtentionList__ = ['xsltExtExample', ]
32__xsltExtentionList__ = []
33
34import os, sys, re, time, datetime, shutil, urllib.request, urllib.parse, urllib.error, string
35from copy import deepcopy
36import io
37
38class OutStreamEncoder(object):
39 """Wraps a stream with an encoder"""
40 def __init__(self, outstream, encoding=None):
41 self.out = outstream
42 if not encoding:
43 self.encoding = sys.getfilesystemencoding()
44 else:
45 self.encoding = encoding
46
47 def write(self, obj):
48 """Wraps the output stream, encoding Unicode strings with the specified encoding"""
49 if isinstance(obj, str):
50 obj = obj.encode(self.encoding)
51 try:
52 self.out.buffer.write(obj)
53 except OSError:
54 pass
55
56 def __getattr__(self, attr):
57 """Delegate everything but write to the stream"""
58 return getattr(self.out, attr)
59
60if isinstance(sys.stdout, io.TextIOWrapper):
61 sys.stdout = OutStreamEncoder(sys.stdout, 'utf8')
62 sys.stderr = OutStreamEncoder(sys.stderr, 'utf8')
63
64try:
65 from io import StringIO
66 from lxml import etree
67except Exception as e:
68 sys.stderr.write('\n! Error - Importing the "lxml" and "StringIO" python libraries failed on error(%s)\n' % e)
69 sys.exit(1)
70
71
72class xpathFunctions(object):
73 """Functions specific extending XPath
74 """
75 def __init__(self):
76 self.functList = ['chrisPirilloLinkGeneration', ]
77 self.TextTail = etree.XPath("string()")
78 self.namespaces = {
79 'content': "http://purl.org/rss/1.0/modules/content/",
80 'wfw': "http://wellformedweb.org/CommentAPI/",
81 'dc': "http://purl.org/dc/elements/1.1/",
82 'atom': "http://www.w3.org/2005/Atom",
83 'sy': "http://purl.org/rss/1.0/modules/syndication/",
84 'slash': "http://purl.org/rss/1.0/modules/slash/",
85 'itunes': "http://www.itunes.com/dtds/podcast-1.0.dtd",
86 'media': "http://search.yahoo.com/mrss/",
87 'feedburner': "http://rssnamespace.org/feedburner/ext/1.0",
88 'atom10': "http://www.w3.org/2005/Atom",
89 }
90 self.youtubeFilter = etree.XPath('.//embed/@src', namespaces=self.namespaces)
91 # end __init__()
92
93
98
99 def chrisPirilloLinkGeneration(self, context, *arg):
100 '''Generate a link for the video.
101 Call example:
102 'mnvXpath:chrisPirilloLinkGeneration(normalize-space(link), normalize-space(description))'
103 return the url link
104 '''
105 tmpHtml = common.getHtmlData(*arg)
106 fullScreenLink = self.youtubeFilter(tmpHtml)
107 if len(fullScreenLink):
108 link = '%s%s' % (fullScreenLink[0], '&autoplay=1')
109 return link
110 return arg[0]
111 # end chrisPirilloLinkGeneration()
112
113
118
119
124
125
def chrisPirilloLinkGeneration(self, context, *arg)
Start of XPath extension functions.