MythTV  master
lyricswiki.py
Go to the documentation of this file.
1 #-*- coding: UTF-8 -*-
2 
3 import sys, re, socket
4 
5 try:
6  from urllib2 import quote, urlopen, HTTPError
7 except ImportError:
8  from urllib.request import urlopen, HTTPError
9  from urllib.parse import quote
10 
11 try:
12  import HTMLParser as html_parser
13 except ImportError:
14  from html import parser as html_parser
15 
16 from optparse import OptionParser
17 
18 if sys.version_info < (2, 7):
19  import simplejson
20 else:
21  import json as simplejson
22 
23 from common import *
24 
25 __author__ = "Paul Harrison and ronie'"
26 __title__ = "LyricsWiki"
27 __description__ = "Search http://lyrics.wikia.com for lyrics"
28 __priority__ = "150"
29 __version__ = "0.2"
30 __syncronized__ = False
31 
32 
33 LIC_TXT = 'we are not licensed to display the full lyrics for this song at the moment'
34 
35 debug = False
36 
37 socket.setdefaulttimeout(10)
38 
40  def __init__( self ):
41  self.url = 'http://lyrics.wikia.com/api.php?func=getSong&artist=%s&song=%s&fmt=realjson'
42 
43  def get_lyrics(self, lyrics):
44  utilities.log(debug, "%s: searching lyrics for %s - %s - %s" % (__title__, lyrics.artist, lyrics.album, lyrics.title))
45 
46  try:
47  req = urlopen(self.url % (quote(lyrics.artist), quote(lyrics.title)))
48  response = req.read().decode('utf-8')
49  except:
50  return False
51  req.close()
52  data = simplejson.loads(response)
53  try:
54  self.page = data['url']
55  except:
56  return False
57  if not self.page.endswith('action=edit'):
58  utilities.log(debug, "%s: search url: %s" % (__title__, self.page))
59  try:
60  req = urlopen(self.page)
61  response = req.read().decode('utf-8')
62  except HTTPError as error: # strange... sometimes lyrics are returned with a 404 error
63  if error.code == 404:
64  response = error.read().decode('utf-8')
65  else:
66  return False
67  req.close()
68  matchcode = re.search("lyricbox'>.*?(&#.*?)<div", response)
69  try:
70  lyricscode = (matchcode.group(1))
71  htmlparser = html_parser.HTMLParser()
72  lyricstext = htmlparser.unescape(lyricscode).replace('<br />', '\n')
73  lyr = re.sub('<[^<]+?>', '', lyricstext)
74  if LIC_TXT in lyr:
75  return False
76  lyrics.lyrics = lyr
77  return True
78  except:
79  return False
80  else:
81  return False
82 
84  found = False
85  lyrics = utilities.Lyrics()
86  lyrics.source = __title__
87  lyrics.syncronized = __syncronized__
88  lyrics.artist = 'Dire Straits'
89  lyrics.album = 'Brothers In Arms'
90  lyrics.title = 'Money For Nothing'
91 
92  fetcher = LyricsFetcher()
93  found = fetcher.get_lyrics(lyrics)
94 
95  if found:
96  utilities.log(True, "Everything appears in order.")
97  buildLyrics(lyrics)
98  sys.exit(0)
99 
100  utilities.log(True, "The lyrics for the test search failed!")
101  sys.exit(1)
102 
103 def buildLyrics(lyrics):
104  from lxml import etree
105  xml = etree.XML(u'<lyrics></lyrics>')
106  etree.SubElement(xml, "artist").text = lyrics.artist
107  etree.SubElement(xml, "album").text = lyrics.album
108  etree.SubElement(xml, "title").text = lyrics.title
109  etree.SubElement(xml, "syncronized").text = 'True' if __syncronized__ else 'False'
110  etree.SubElement(xml, "grabber").text = lyrics.source
111 
112  lines = lyrics.lyrics.splitlines()
113  for line in lines:
114  etree.SubElement(xml, "lyric").text = line
115 
116  utilities.log(True, utilities.convert_etree(etree.tostring(xml, encoding='UTF-8',
117  pretty_print=True, xml_declaration=True)))
118  sys.exit(0)
119 
121  from lxml import etree
122  version = etree.XML(u'<grabber></grabber>')
123  etree.SubElement(version, "name").text = __title__
124  etree.SubElement(version, "author").text = __author__
125  etree.SubElement(version, "command").text = 'lyricswiki.py'
126  etree.SubElement(version, "type").text = 'lyrics'
127  etree.SubElement(version, "description").text = __description__
128  etree.SubElement(version, "version").text = __version__
129  etree.SubElement(version, "priority").text = __priority__
130  etree.SubElement(version, "syncronized").text = 'True' if __syncronized__ else 'False'
131 
132  utilities.log(True, utilities.convert_etree(etree.tostring(version, encoding='UTF-8',
133  pretty_print=True, xml_declaration=True)))
134  sys.exit(0)
135 
136 def main():
137  global debug
138 
139  parser = OptionParser()
140 
141  parser.add_option('-v', "--version", action="store_true", default=False,
142  dest="version", help="Display version and author")
143  parser.add_option('-t', "--test", action="store_true", default=False,
144  dest="test", help="Test grabber with a know good search")
145  parser.add_option('-s', "--search", action="store_true", default=False,
146  dest="search", help="Search for lyrics.")
147  parser.add_option('-a', "--artist", metavar="ARTIST", default=None,
148  dest="artist", help="Artist of track.")
149  parser.add_option('-b', "--album", metavar="ALBUM", default=None,
150  dest="album", help="Album of track.")
151  parser.add_option('-n', "--title", metavar="TITLE", default=None,
152  dest="title", help="Title of track.")
153  parser.add_option('-f', "--filename", metavar="FILENAME", default=None,
154  dest="filename", help="Filename of track.")
155  parser.add_option('-d', '--debug', action="store_true", default=False,
156  dest="debug", help=("Show debug messages"))
157 
158  opts, args = parser.parse_args()
159 
160  lyrics = utilities.Lyrics()
161  lyrics.source = __title__
162  lyrics.syncronized = __syncronized__
163 
164  if opts.debug:
165  debug = True
166 
167  if opts.version:
168  buildVersion()
169 
170  if opts.test:
172 
173  if opts.artist:
174  lyrics.artist = opts.artist
175  if opts.album:
176  lyrics.album = opts.album
177  if opts.title:
178  lyrics.title = opts.title
179  if opts.filename:
180  lyrics.filename = opts.filename
181 
182  fetcher = LyricsFetcher()
183  if fetcher.get_lyrics(lyrics):
184  buildLyrics(lyrics)
185  sys.exit(0)
186  else:
187  utilities.log(True, "No lyrics found for this track")
188  sys.exit(1)
189 
190 
191 if __name__ == '__main__':
192  main()
lyricswiki.LyricsFetcher
Definition: lyricswiki.py:39
lyricswiki.LyricsFetcher.__init__
def __init__(self)
Definition: lyricswiki.py:40
lyricswiki.main
def main()
Definition: lyricswiki.py:136
decode
static int decode(unsigned char *vbiline, int scale0, int scale1)
Definition: cc.cpp:70
lyricswiki.LyricsFetcher.url
url
Definition: lyricswiki.py:41
lyricswiki.buildLyrics
def buildLyrics(lyrics)
Definition: lyricswiki.py:103
lyricswiki.performSelfTest
def performSelfTest()
Definition: lyricswiki.py:83
lyricswiki.buildVersion
def buildVersion()
Definition: lyricswiki.py:120
lyricswiki.LyricsFetcher.page
page
Definition: lyricswiki.py:54
lyricswiki.LyricsFetcher.get_lyrics
def get_lyrics(self, lyrics)
Definition: lyricswiki.py:43