/mythtv/programs/scripts/internetcontent/nv_python_libs/xsltfunctions/comedycentral_api.py

https://github.com/JackOfMostTrades/mythtv
Python | 134 lines | 106 code | 7 blank | 21 comment | 2 complexity | 766e3ef3ee91a929126244d680e0cba7 MD5 | raw file
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. # ----------------------
  4. # Name: comedycentral_api - XPath and XSLT functions for the Comedy Central 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__ ="comedycentral_api - XPath and XSLT functions for the Comedy Central RSS/HTML"
  16. __author__="R.D. Vaughan"
  17. __purpose__='''
  18. This python script is intended to perform a variety of utility functions
  19. for the conversion of data to the MNV standard RSS output format.
  20. See this link for the specifications:
  21. http://www.mythtv.org/wiki/MythNetvision_Grabber_Script_Format
  22. '''
  23. __version__="v0.1.0"
  24. # 0.1.0 Initial development
  25. # Specify the class names that have XPath extention functions
  26. __xpathClassList__ = ['xpathFunctions', ]
  27. # Specify the XSLT extention class names. Each class is a stand lone extention function
  28. #__xsltExtentionList__ = ['xsltExtExample', ]
  29. __xsltExtentionList__ = []
  30. import os, sys, re, time, datetime, shutil, urllib, string
  31. from copy import deepcopy
  32. class OutStreamEncoder(object):
  33. """Wraps a stream with an encoder"""
  34. def __init__(self, outstream, encoding=None):
  35. self.out = outstream
  36. if not encoding:
  37. self.encoding = sys.getfilesystemencoding()
  38. else:
  39. self.encoding = encoding
  40. def write(self, obj):
  41. """Wraps the output stream, encoding Unicode strings with the specified encoding"""
  42. if isinstance(obj, unicode):
  43. try:
  44. self.out.write(obj.encode(self.encoding))
  45. except IOError:
  46. pass
  47. else:
  48. try:
  49. self.out.write(obj)
  50. except IOError:
  51. pass
  52. def __getattr__(self, attr):
  53. """Delegate everything but write to the stream"""
  54. return getattr(self.out, attr)
  55. sys.stdout = OutStreamEncoder(sys.stdout, 'utf8')
  56. sys.stderr = OutStreamEncoder(sys.stderr, 'utf8')
  57. try:
  58. from StringIO import StringIO
  59. from lxml import etree
  60. except Exception, e:
  61. sys.stderr.write(u'\n! Error - Importing the "lxml" and "StringIO" python libraries failed on error(%s)\n' % e)
  62. sys.exit(1)
  63. # Check that the lxml library is current enough
  64. # From the lxml documents it states: (http://codespeak.net/lxml/installation.html)
  65. # "If you want to use XPath, do not use libxml2 2.6.27. We recommend libxml2 2.7.2 or later"
  66. # Testing was performed with the Ubuntu 9.10 "python-lxml" version "2.1.5-1ubuntu2" repository package
  67. version = ''
  68. for digit in etree.LIBXML_VERSION:
  69. version+=str(digit)+'.'
  70. version = version[:-1]
  71. if version < '2.7.2':
  72. sys.stderr.write(u'''
  73. ! Error - The installed version of the "lxml" python library "libxml" version is too old.
  74. At least "libxml" version 2.7.2 must be installed. Your version is (%s).
  75. ''' % version)
  76. sys.exit(1)
  77. class xpathFunctions(object):
  78. """Functions specific extending XPath
  79. """
  80. def __init__(self):
  81. self.functList = ['comedycentralMakeLink', ]
  82. self.persistence = {}
  83. # end __init__()
  84. ######################################################################################################
  85. #
  86. # Start of XPath extension functions
  87. #
  88. ######################################################################################################
  89. def comedycentralMakeLink(self, context, *arg):
  90. '''Parse the item element and extract a Web link
  91. Call example: 'mnvXpath:comedycentralMakeLink(.)'
  92. return a URL link to the item web page
  93. '''
  94. tmpTitle = arg[0][0].find('title').text.strip()
  95. tmpVideoCode = arg[0][0].find('guid').text.strip()
  96. index = tmpVideoCode.rfind('.')
  97. if index != -1:
  98. tmpVideoCode = tmpVideoCode[index+1:]
  99. tmpLink = common.linkWebPage('dummy', 'comedycentral').replace(u'TITLE', urllib.quote(tmpTitle)).replace(u'VIDEOCODE', tmpVideoCode)
  100. return tmpLink.strip()
  101. # end comedycentralMakeLink()
  102. ######################################################################################################
  103. #
  104. # End of XPath extension functions
  105. #
  106. ######################################################################################################
  107. ######################################################################################################
  108. #
  109. # Start of XSLT extension functions
  110. #
  111. ######################################################################################################
  112. ######################################################################################################
  113. #
  114. # End of XSLT extension functions
  115. #
  116. ######################################################################################################