MythTV master
skyAtNight_api.py
Go to the documentation of this file.
1# -*- coding: UTF-8 -*-
2
3# ----------------------
4# Name: skyAtNight_api - XPath and XSLT functions for the BBC Sky at Night magazine 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__ ="skyAtNight_api - XPath and XSLT functions for the BBC Sky at Night magazine 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
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 = ['skyAtNightTitleEp', ]
78 # end __init__()
79
80
85
86 def skyAtNightTitleEp(self, context, *arg):
87 '''Parse the guid element and extract an episode number
88 Call example: 'mnvXpath:skyAtNightTitleEp(string(guid))'
89 return the a massaged title element and an episode element in an array
90 '''
91 searchText = 'BBC_SAN'
92 title = arg[0]
93 episodeNumber = title.replace(searchText, '').strip()
94 try:
95 episodeNumber = int(episodeNumber)
96 except:
97 episodeNumber = None
98
99 mythtvNamespace = "http://www.mythtv.org/wiki/MythNetvision_Grabber_Script_Format"
100 mythtv = "{%s}" % mythtvNamespace
101 NSMAP = {'mythtv' : mythtvNamespace}
102 elementTmp = etree.Element(mythtv + "mythtv", nsmap=NSMAP)
103 if not episodeNumber is None:
104 etree.SubElement(elementTmp, "title").text = "EP%02d" % episodeNumber
105 etree.SubElement(elementTmp, mythtv + "episode").text = "%s" % episodeNumber
106 else:
107 etree.SubElement(elementTmp, "title").text = title
108 return elementTmp
109 # end skyAtNightTitleEp()
110
111
116
117
122
123
def skyAtNightTitleEp(self, context, *arg)
Start of XPath extension functions.