summaryrefslogtreecommitdiff
path: root/plugins/Tabs.py
diff options
context:
space:
mode:
authorjerous <jerous@gmail.com>2008-06-13 18:51:31 +0200
committerjerous <jerous@gmail.com>2008-06-13 18:51:31 +0200
commit4119943efd6ff43fb2e15389102c61687ce94764 (patch)
treeb2d6864ee8b3adf69cabd199350797d9ca1f525c /plugins/Tabs.py
parent2a2ae8aae97499b24688295babf3b0c889630b09 (diff)
plugin for fetching tabs
Diffstat (limited to 'plugins/Tabs.py')
-rw-r--r--plugins/Tabs.py157
1 files changed, 157 insertions, 0 deletions
diff --git a/plugins/Tabs.py b/plugins/Tabs.py
new file mode 100644
index 0000000..be485e0
--- /dev/null
+++ b/plugins/Tabs.py
@@ -0,0 +1,157 @@
+from PyQt4 import QtGui,QtCore
+
+import re
+from thread import start_new_thread
+from traceback import print_exc
+
+from misc import *
+from clSettings import settings,mpdSettings
+from clMonty import monty
+from clPlugin import *
+
+class ResetEvent(QtCore.QEvent):
+ song=None
+ def __init__(self, song=None):
+ QtCore.QEvent.__init__(self,QtCore.QEvent.User)
+ self.song=song
+class AddHtmlEvent(QtCore.QEvent):
+ html=None
+ def __init__(self,html):
+ QtCore.QEvent.__init__(self,QtCore.QEvent.User)
+ self.html=html
+
+TABS_DEFAULT_ENGINE='http://www.google.com/search?q=tabs|chords+"$artist"+"$title"'
+TABS_DEFAULT_SITES='azchords.com <pre>(.*?)</pre>\n'\
+ 'fretplay.com <P CLASS="tabs">(.*?)</P>\n'\
+ 'guitaretab.com <pre style="COLOR: #000000; FONT-SIZE: 11px;">(.*)?</pre>'\
+
+class wgTabs(QtGui.QWidget):
+ " contains the tabs"
+ txt=None
+ def __init__(self, parent=None):
+ QtGui.QWidget.__init__(self, parent)
+ self.txt=QtGui.QTextEdit(parent)
+ self.txt.setReadOnly(True)
+
+ layout=QtGui.QVBoxLayout()
+ layout.addWidget(self.txt)
+ self.setLayout(layout)
+
+ monty.addListener('onSongChange', self.onSongChange)
+ monty.addListener('onReady', self.onReady)
+ monty.addListener('onDisconnect', self.onDisconnect)
+
+ def onSongChange(self, params):
+ song=monty.getCurrentSong()
+ try:
+ song._data['file']
+ except:
+ self.resetTxt()
+ return
+
+ self.resetTxt(song)
+ start_new_thread(self.fetchTabs, (song,))
+
+ def customEvent(self, event):
+ if isinstance(event,ResetEvent):
+ self.resetTxt(event.song)
+ elif isinstance(event,AddHtmlEvent):
+ self.txt.insertHtml(event.html)
+
+ def onReady(self, params):
+ self.onSongChange(None)
+
+ _mutex=QtCore.QMutex()
+ _fetchCnt=0
+ def fetchTabs(self, song):
+ # only allow 1 instance to look tabs!
+ self._mutex.lock()
+ if self._fetchCnt:
+ self._mutex.unlock()
+ return
+ self._fetchCnt=1
+ self._mutex.unlock()
+
+ QtCore.QCoreApplication.postEvent(self, ResetEvent(song))
+
+ # save the data to file!
+ save_dir=settings.get('tabs.dir', '/holy_grail')
+ fInfo=QtCore.QFileInfo(save_dir)
+ if fInfo.isDir():
+ lyFName='%s/%s - %s.txt'%(save_dir,song.getArtist(),song.getTitle())
+ else:
+ lyFName=None
+ # does the file exist? if yes, read that one!
+ try:
+ # we have it: load, and return!
+ file=open(lyFName, 'r')
+ QtCore.QCoreApplication.postEvent(self, AddHtmlEvent(file.read()))
+ file.close()
+ self._fetchCnt=0
+ return
+ except:
+ pass
+
+ # fetch from inet
+ QtCore.QCoreApplication.postEvent(self, AddHtmlEvent('<i>Searching tabs ...</i>'))
+
+ lines=settings.get('tabs.sites', TABS_DEFAULT_SITES).split('\n')
+ sites={}
+ for line in lines:
+ if line.strip():
+ sites[line[0:line.find('\t')]]=line[line.find('\t'):].strip()
+ # construct URL to search!
+ SE=settings.get('tabs.engine', TABS_DEFAULT_ENGINE)
+ try:
+ ret=fetch(SE, sites, song, {})
+ if ret:
+ txt='<pre>%s<br /><br /><a href="%s">%s</a></pre>'%(ret[0],ret[1],ret[1])
+ # save for later use!
+ if lyFName:
+ # we can't save if the path isn't correct
+ file=open(lyFName, 'w')
+ file.write(ret[0])
+ file.close()
+ else:
+ txt="No tabs found :'("
+
+ QtCore.QCoreApplication.postEvent(self, ResetEvent(song))
+ QtCore.QCoreApplication.postEvent(self, AddHtmlEvent(txt))
+ except:
+ print_exc()
+ QtCore.QCoreApplication.postEvent(self, ResetEvent(song))
+ QtCore.QCoreApplication.postEvent(self, AddHtmlEvent('Woops, site unavailable!'\
+ '<br />You have an internet connection?'))
+ self._fetchCnt=0
+
+ def onDisconnect(self, params):
+ self.resetTxt()
+
+ def resetTxt(self, song=None):
+ self.txt.clear()
+ if song:
+ self.txt.insertHtml('<b>%s</b>\n<br /><u>%s</u><br />'\
+ '<br />\n\n'%(song.getTitle(), song.getArtist()))
+
+
+class pluginTabs(Plugin):
+ o=None
+ def __init__(self, winMain):
+ Plugin.__init__(self, winMain, 'Tabs')
+ self.o=wgTabs(None)
+ def getInfo(self):
+ return "Show (and fetch) the tabs of the currently playing song."
+
+ def _getDockWidget(self):
+ return self._createDock(self.o)
+
+ def _getSettings(self):
+ sites=QtGui.QTextEdit()
+ sites.insertPlainText(settings.get('tabs.sites', TABS_DEFAULT_SITES))
+ return [
+ ['tabs.engine', 'Search engine', 'The URL that is used to search. $artist, $title and $album are replaced in the URL.', QtGui.QLineEdit(settings.get('tabs.engine', TABS_DEFAULT_ENGINE))],
+ ['tabs.sites', 'Sites & regexes', 'This field contains all sites, together with the regex needed to fetch the tabs.\nEvery line must look like this: $domain $regex-start(.*?)$regex-end\n$domain is the domain of the tabs website, $regex-start is the regex indicating the start of the tabs, $regex-end indicates the end. E.g. footabs.org <tabs>(.*?)</tabs>', sites],
+ ['tabs.dir', 'Tabs directory', 'Directory where tabs should be stored and retrieved.', QtGui.QLineEdit(settings.get('tabs.dir'))],
+ ]
+ def afterSaveSettings(self):
+ self.o.onSongChange(None)