MythTV  master
culrcwrap.py
Go to the documentation of this file.
1 # -*- Mode: python; coding: utf-8; indent-tabs-mode: nil; -*-
2 """
3 Wrapper for using CU LRC scrapers with MythMusic of MythTV
4 
5 Paul Harrison, ronie, Timothy Witham
6 """
7 
8 # UPDATE THIS from https://gitlab.com/ronie/script.cu.lrclyrics
9 # second line of its addon.xml:
10 __version__ = '6.6.8'
11 
12 # simulate kodi/xbmc via very simplified Kodistubs.
13 import os
14 import sys
15 sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)) + '/../Kodistubs')
16 from lib.utils import *
17 from optparse import OptionParser
18 
19 # album is never searched, but it is given in the xml to mythtv
20 class Song(Song):
21  def __init__(self, *args, **kwargs):
22  super().__init__(*args, **kwargs) # from ../lib/utils.py
23  self.album = ''
24 
25 debug = False
26 
27 lyricssettings = {}
28 lyricssettings['debug'] = ADDON.getSettingBool('log_enabled')
29 lyricssettings['save_filename_format'] = ADDON.getSettingInt('save_filename_format')
30 lyricssettings['save_lyrics_path'] = ADDON.getSettingString('save_lyrics_path')
31 lyricssettings['save_subfolder'] = ADDON.getSettingBool('save_subfolder')
32 lyricssettings['save_subfolder_path'] = ADDON.getSettingString('save_subfolder_path')
33 
35  confdir = os.environ.get('MYTHCONFDIR', '')
36 
37  if (not confdir) or (confdir == '/'):
38  confdir = os.environ.get('HOME', '')
39 
40  if (not confdir) or (confdir == '/'):
41  log("Unable to find MythTV directory for metadata cache.", debug=True)
42  return '/tmp'
43 
44  confdir = os.path.join(confdir, '.mythtv')
45  cachedir = os.path.join(confdir, 'cache')
46 
47  if not os.path.exists(cachedir):
48  os.makedirs(cachedir)
49 
50  return cachedir
51 
53  try:
54  from bs4 import BeautifulSoup
55  except:
56  log("Failed to import BeautifulSoup. This grabber requires python-bs4", debug=True)
57  sys.exit(1)
58 
59  found = False
60  song = Song(opt=lyricssettings)
61  song.artist = about.get('artist')
62  song.title = about.get('title')
63  song.album = about.get('album')
64  song.filepath = about.get('filename')
65 
66  fetcher = LyricsFetcher(settings=lyricssettings, debug=True)
67  lyrics = fetcher.get_lyrics(song)
68 
69  if lyrics:
70  if debug:
71  print(lyrics.lyrics)
72  try:
73  buildLyrics(song, lyrics)
74  except:
75  log("Failed to build lyrics xml file. "
76  "Maybe you don't have lxml installed?", debug=True)
77  sys.exit(1)
78 
79  log("Everything appears in order.", debug=True)
80  sys.exit(0)
81 
82  log("Failed to find the lyrics for the test search!", debug=True)
83  sys.exit(1)
84 
85 def buildLyrics(song, lyrics):
86  from lxml import etree
87  xml = etree.XML(u'<lyrics></lyrics>')
88  etree.SubElement(xml, "artist").text = song.artist
89  etree.SubElement(xml, "album").text = song.album
90  etree.SubElement(xml, "title").text = song.title
91  etree.SubElement(xml, "syncronized").text = 'True' if about['syncronized'] else 'False'
92  etree.SubElement(xml, "grabber").text = about['name']
93 
94  lines = lyrics.lyrics.splitlines()
95  for line in lines:
96  etree.SubElement(xml, "lyric").text = line
97 
98  print(etree.tostring(xml, encoding='UTF-8',
99  pretty_print=True, xml_declaration=True).decode())
100 
102  from lxml import etree
103  xml = etree.XML(u'<grabber></grabber>')
104  etree.SubElement(xml, "name").text = about['name']
105  etree.SubElement(xml, "author").text = about['author']
106  etree.SubElement(xml, "command").text = about['command']
107  etree.SubElement(xml, "type").text = 'lyrics'
108  etree.SubElement(xml, "description").text = about['description']
109  etree.SubElement(xml, "version").text = about['version']
110  etree.SubElement(xml, "priority").text = about['priority']
111  etree.SubElement(xml, "syncronized").text = 'True' if about['syncronized'] else 'False'
112 
113  print(etree.tostring(xml, encoding='UTF-8',
114  pretty_print=True, xml_declaration=True).decode())
115  sys.exit(0)
116 
117 def main(filename, info, fetcher):
118  global debug
119  global about
120  about = info
121  about['command'] = os.path.basename(filename)
122  if not about.get('version'):
123  about['version'] = __version__
124  if not about.get('album'):
125  about['album'] = ''
126  global LyricsFetcher
127  LyricsFetcher = fetcher
128 
129  parser = OptionParser()
130 
131  parser.add_option('-v', "--version", action="store_true", default=False,
132  dest="version", help="Display version and author")
133  parser.add_option('-t', "--test", action="store_true", default=False,
134  dest="test", help="Test grabber with a known good search")
135  parser.add_option('-s', "--search", action="store_true", default=False,
136  dest="search", help="Search for lyrics.")
137  parser.add_option('-a', "--artist", metavar="ARTIST", default=None,
138  dest="artist", help="Artist of track.")
139  parser.add_option('-b', "--album", metavar="ALBUM", default=None,
140  dest="album", help="Album of track.")
141  parser.add_option('-n', "--title", metavar="TITLE", default=None,
142  dest="title", help="Title of track.")
143  parser.add_option('-f', "--filename", metavar="FILENAME", default=None,
144  dest="filename", help="Filename of track.")
145  parser.add_option('-d', '--debug', action="store_true", default=False,
146  dest="debug", help=("Show debug messages"))
147 
148  opts, args = parser.parse_args()
149 
150  song = Song(opt=lyricssettings)
151 
152  if opts.debug:
153  debug = True
154 
155  if opts.version:
156  buildVersion()
157 
158  if opts.test:
160 
161  if opts.artist:
162  song.artist = opts.artist
163  if opts.album:
164  song.album = opts.album
165  if opts.title:
166  song.title = opts.title
167  if opts.filename:
168  song.filepath = opts.filename
169 
170  fetcher = LyricsFetcher(settings=lyricssettings, debug=debug)
171  lyrics = fetcher.get_lyrics(song)
172  if lyrics:
173  buildLyrics(song, lyrics)
174  sys.exit(0)
175  else:
176  log("No lyrics found for this track", debug=True)
177  sys.exit(1)
common.culrcwrap.buildVersion
def buildVersion()
Definition: culrcwrap.py:101
common.culrcwrap.getCacheDir
def getCacheDir()
Definition: culrcwrap.py:34
utils
common.culrcwrap.performSelfTest
def performSelfTest()
Definition: culrcwrap.py:52
common.culrcwrap.buildLyrics
def buildLyrics(song, lyrics)
Definition: culrcwrap.py:85
print
static void print(const QList< uint > &raw_minimas, const QList< uint > &raw_maximas, const QList< float > &minimas, const QList< float > &maximas)
Definition: vbi608extractor.cpp:29
common.culrcwrap.Song.album
album
Definition: culrcwrap.py:23
common.culrcwrap.Song
Definition: culrcwrap.py:20
common.culrcwrap.Song.__init__
def __init__(self, *args, **kwargs)
Definition: culrcwrap.py:21
common.culrcwrap.main
def main(filename, info, fetcher)
Definition: culrcwrap.py:117
xbmc.log
None log(str msg, int level=LOGDEBUG)
Definition: xbmc.py:9