MythTV  master
filelyrics.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: UTF-8 -*-
3 # ----------------------
4 """
5 Scraper for file lyrics
6 """
7 
8 import sys, os, re, chardet
9 import xml.dom.minidom as xml
10 from optparse import OptionParser
11 from common import *
12 
13 __author__ = "Paul Harrison"
14 __title__ = "FileLyrics"
15 __description__ = "Search the same directory as the track for lyrics"
16 __version__ = "0.1"
17 __priority__ = "105"
18 __syncronized__ = True
19 
20 debug = False
21 
22 
24 
25 
26  def get_lyrics(self, lyrics):
27  utilities.log(debug, "%s: searching lyrics for %s - %s - %s" % (__title__, lyrics.artist, lyrics.album, lyrics.title))
28 
29  if utilities.IS_PY2:
30  filename = lyrics.filename.decode("utf-8")
31  else:
32  filename = lyrics.filename
33  filename = os.path.splitext(filename)[0]
34 
35  # look for a file ending in .lrc with the same filename as the track minus the extension
36  lyricFile = filename + '.lrc'
37  utilities.log(debug, "%s: searching for lyrics file: %s " % (__title__, lyricFile))
38  if os.path.exists(lyricFile) and os.path.isfile(lyricFile):
39  #load the text file
40  with open (lyricFile, "r") as f:
41  lines = f.readlines()
42 
43  for line in lines:
44  lyrics.lyrics += line
45 
46  return True
47 
48  return False;
49 
51  found = False
52  lyrics = utilities.Lyrics()
53  lyrics.source = __title__
54  lyrics.syncronized = __syncronized__
55  lyrics.artist = 'Robb Benson'
56  lyrics.album = 'Demo Tracks'
57  lyrics.title = 'Lone Rock'
58  lyrics.filename = os.path.dirname(os.path.abspath(__file__)) + '/examples/filelyrics.mp3'
59  fetcher = LyricsFetcher()
60  found = fetcher.get_lyrics(lyrics)
61 
62  if found:
63  try:
64  buildLyrics(lyrics)
65  except:
66  utilities.log(True, "Failed to build lyrics xml file. "
67  "Maybe you don't have lxml installed?")
68  sys.exit(1)
69 
70  utilities.log(True, "Everything appears in order.")
71  sys.exit(0)
72 
73  utilities.log(True, "Failed to find the lyrics for the test search!")
74  sys.exit(1)
75 
76 def buildLyrics(lyrics):
77  from lxml import etree
78  xml = etree.XML(u'<lyrics></lyrics>')
79  etree.SubElement(xml, "artist").text = lyrics.artist
80  etree.SubElement(xml, "album").text = lyrics.album
81  etree.SubElement(xml, "title").text = lyrics.title
82  etree.SubElement(xml, "syncronized").text = 'True' if __syncronized__ else 'False'
83  etree.SubElement(xml, "grabber").text = lyrics.source
84 
85  lines = lyrics.lyrics.splitlines()
86  for line in lines:
87  etree.SubElement(xml, "lyric").text = line
88 
89  utilities.log(True, utilities.convert_etree(etree.tostring(xml, encoding='UTF-8',
90  pretty_print=True, xml_declaration=True)))
91 
93  from lxml import etree
94  version = etree.XML(u'<grabber></grabber>')
95  etree.SubElement(version, "name").text = __title__
96  etree.SubElement(version, "author").text = __author__
97  etree.SubElement(version, "command").text = 'alsong.py'
98  etree.SubElement(version, "type").text = 'lyrics'
99  etree.SubElement(version, "description").text = __description__
100  etree.SubElement(version, "version").text = __version__
101  etree.SubElement(version, "priority").text = __priority__
102  etree.SubElement(version, "syncronized").text = 'True' if __syncronized__ else 'False'
103 
104  utilities.log(True, utilities.convert_etree(etree.tostring(version, encoding='UTF-8',
105  pretty_print=True, xml_declaration=True)))
106  sys.exit(0)
107 
108 def main():
109  global debug
110 
111  parser = OptionParser()
112 
113  parser.add_option('-v', "--version", action="store_true", default=False,
114  dest="version", help="Display version and author")
115  parser.add_option('-t', "--test", action="store_true", default=False,
116  dest="test", help="Perform self-test for dependencies.")
117  parser.add_option('-s', "--search", action="store_true", default=False,
118  dest="search", help="Search for lyrics.")
119  parser.add_option('-a', "--artist", metavar="ARTIST", default=None,
120  dest="artist", help="Artist of track.")
121  parser.add_option('-b', "--album", metavar="ALBUM", default=None,
122  dest="album", help="Album of track.")
123  parser.add_option('-n', "--title", metavar="TITLE", default=None,
124  dest="title", help="Title of track.")
125  parser.add_option('-f', "--filename", metavar="FILENAME", default=None,
126  dest="filename", help="Filename of track.")
127  parser.add_option('-d', '--debug', action="store_true", default=False,
128  dest="debug", help=("Show debug messages"))
129 
130  opts, args = parser.parse_args()
131 
132  lyrics = utilities.Lyrics()
133  lyrics.source = __title__
134  lyrics.syncronized = __syncronized__
135 
136  if opts.debug:
137  debug = True
138 
139  if opts.version:
140  buildVersion()
141 
142  if opts.test:
144 
145  if opts.artist:
146  lyrics.artist = opts.artist
147  if opts.album:
148  lyrics.album = opts.album
149  if opts.title:
150  lyrics.title = opts.title
151  if opts.filename:
152  lyrics.filename = opts.filename
153 
154  fetcher = LyricsFetcher()
155  if fetcher.get_lyrics(lyrics):
156  buildLyrics(lyrics)
157  sys.exit(0)
158  else:
159  utilities.log(True, "No lyrics found for this track")
160  sys.exit(1)
161 
162 if __name__ == '__main__':
163  main()
filelyrics.main
def main()
Definition: filelyrics.py:108
filelyrics.LyricsFetcher.get_lyrics
def get_lyrics(self, lyrics)
Definition: filelyrics.py:26
filelyrics.LyricsFetcher
Definition: filelyrics.py:23
filelyrics.performSelfTest
def performSelfTest()
Definition: filelyrics.py:50
filelyrics.buildLyrics
def buildLyrics(lyrics)
Definition: filelyrics.py:76
filelyrics.buildVersion
def buildVersion()
Definition: filelyrics.py:92