[weboob] [PATCH 1/2] New application : taboob

Rudy Weber webrudy at gmail.com
Sun Mar 2 23:26:52 CET 2014


Signed-off-by: Rudy Weber <webrudy at gmail.com>
---
 scripts/taboob                         |   26 +++++++
 weboob/applications/taboob/__init__.py |   23 ++++++
 weboob/applications/taboob/taboob.py   |  132 ++++++++++++++++++++++++++++++++
 weboob/capabilities/tab.py             |   67 ++++++++++++++++
 4 files changed, 248 insertions(+)
 create mode 100755 scripts/taboob
 create mode 100644 weboob/applications/taboob/__init__.py
 create mode 100644 weboob/applications/taboob/taboob.py
 create mode 100644 weboob/capabilities/tab.py

diff --git a/scripts/taboob b/scripts/taboob
new file mode 100755
index 0000000..9596247
--- /dev/null
+++ b/scripts/taboob
@@ -0,0 +1,26 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# Copyright(C) 2014 Rudy Weber
+#
+# This file is part of weboob.
+#
+# weboob is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# weboob is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with weboob. If not, see <http://www.gnu.org/licenses/>.
+
+
+from weboob.applications.taboob import Taboob
+
+
+if __name__ == '__main__':
+    Taboob.run()
diff --git a/weboob/applications/taboob/__init__.py b/weboob/applications/taboob/__init__.py
new file mode 100644
index 0000000..4bc200f
--- /dev/null
+++ b/weboob/applications/taboob/__init__.py
@@ -0,0 +1,23 @@
+# -*- coding: utf-8 -*-
+
+# Copyright(C) 2014 Rudy Weber
+#
+# This file is part of weboob.
+#
+# weboob is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# weboob is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with weboob. If not, see <http://www.gnu.org/licenses/>.
+
+
+from .taboob import Taboob
+
+__all__ = ['Taboob']
diff --git a/weboob/applications/taboob/taboob.py b/weboob/applications/taboob/taboob.py
new file mode 100644
index 0000000..306ef15
--- /dev/null
+++ b/weboob/applications/taboob/taboob.py
@@ -0,0 +1,132 @@
+# -*- coding: utf-8 -*-
+
+# Copyright(C) 2014 Rudy Weber
+#
+# This file is part of weboob.
+#
+# weboob is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# weboob is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with weboob. If not, see <http://www.gnu.org/licenses/>.
+
+
+
+import sys
+import subprocess
+import os
+
+from weboob.capabilities.tab import ICapTab
+from weboob.capabilities.base import empty
+from weboob.tools.application.repl import ReplApplication, defaultcount
+from weboob.tools.application.formatters.iformatter import PrettyFormatter
+
+import urllib
+
+__all__ = ['Taboob', 'TabListFormatter']
+
+
+class TabListFormatter(PrettyFormatter):
+    MANDATORY_FIELDS = ('title', 'artist', 'type')
+
+    def get_title(self, obj):
+        return obj.title
+
+    def get_description(self, obj):
+        artist = u''
+        tabtype = u''
+        if not empty(obj.artist):
+            artist = obj.artist
+        if not empty(obj.type):
+            tabtype = obj.type
+        return artist+' - '+tabtype
+
+
+class Taboob(ReplApplication):
+    APPNAME = 'taboob'
+    VERSION = '0.i'
+    COPYRIGHT = 'Copyright(C) 2014 Rudy Weber'
+    DESCRIPTION = "Console application allowing to search for song tabs on various websites."
+    SHORT_DESCRIPTION = "search and download song tabs"
+    CAPS = ICapTab
+    EXTRA_FORMATTERS = {'tab_list': TabListFormatter}
+    COMMANDS_FORMATTERS = {'search': 'tab_list'}
+
+    def do_download(self, id):
+        """
+        download ID
+
+        Download tab of the song.
+        """
+
+        if not id:
+            print >> sys.stderr, 'This command takes an argument: %s' % self.get_command_help('get', short=True)
+            return
+
+        def check_exec(executable):
+            with open('/dev/null', 'w') as devnull:
+                process = subprocess.Popen(['which', executable], stdout=devnull)
+                if process.wait() != 0:
+                    print >>sys.stderr, 'Please install "%s"' % executable
+                    return False
+            return True
+
+        songtab = self.get_object(id, 'get_tab')
+
+        try:
+            _filename, headers = urllib.urlretrieve(songtab.url)
+            filename = headers['Content-Disposition'].split('=')[1]
+
+            dldir = raw_input('Download path : ')
+            if dldir.endswith('/') == False:
+                dldir += '/'
+
+            if check_exec('wget'):
+                dest = dldir+filename
+                args = ('wget', '-c', songtab.url, '-O', dest)
+                os.spawnlp(os.P_WAIT, args[0], *args)
+        except:
+            print "Cannot download tab"
+
+    def do_show(self, id):
+        """
+        show ID
+
+        Display tab of the song.
+        """
+
+        if not id:
+            print >> sys.stderr, 'This command takes an argument: %s' % self.get_command_help('get', short=True)
+            return 2
+
+        songtab = self.get_object(id, 'get_tab')
+
+        if not songtab.content:
+            print "This tab cannot be displayed. Use download ID instead."
+        else:
+            print songtab.content
+
+    @defaultcount(10)
+    def do_search(self, query):
+        """
+        search [all | chords | bass | tab | gp | powertab | drum | ukulele]  [song title]
+
+        Search tabs by song title and type.
+        """
+
+        criteria, pattern = self.parse_command_args(query, 2, 2)
+        if not pattern or not criteria:
+            print >> sys.stderr, 'This command takes two argument: %s' % self.get_command_help('search', short=True)
+            return 2
+
+        self.change_path([u'search'])
+        self.start_format(pattern=pattern)
+        for backend, songtab in self.do('iter_tab', criteria, pattern):
+            self.cached_format(songtab)
diff --git a/weboob/capabilities/tab.py b/weboob/capabilities/tab.py
new file mode 100644
index 0000000..4e27dbb
--- /dev/null
+++ b/weboob/capabilities/tab.py
@@ -0,0 +1,67 @@
+# -*- coding: utf-8 -*-
+
+# Copyright(C) 2014 Rudy Weber
+#
+# This file is part of weboob.
+#
+# weboob is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# weboob is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with weboob. If not, see <http://www.gnu.org/licenses/>.
+
+
+from .base import IBaseCap, CapBaseObject, StringField
+
+
+__all__ = ['SongTab', 'ICapTab']
+
+
+class SongTab(CapBaseObject):
+    """
+    Song tab object.
+    """
+    title =      StringField('Title of the song')
+    artist =       StringField('Artist of the song')
+    type =       StringField('Type of tab')
+    url =    StringField('Tab url of the song')
+    content =   StringField('Tab of the song')
+
+    def __init__(self, id, title):
+        CapBaseObject.__init__(self, id)
+        self.title = title
+
+
+class ICapTab(IBaseCap):
+    """
+    Tabs websites.
+    """
+    def iter_tab(self, criteria, pattern):
+        """
+        Search tabs by artist or by song
+        and iterate on results.
+
+        :param criteria: 'artist' or 'song'
+        :type criteria: str
+        :param pattern: pattern to search
+        :type pattern: str
+        :rtype: iter[:class:`SongTab`]
+        """
+        raise NotImplementedError()
+
+    def get_tab(self, _id):
+        """
+        Get a tab object from an ID.
+
+        :param _id: ID of tab
+        :type _id: str
+        :rtype: :class:`SongTab`
+        """
+        raise NotImplementedError()
-- 
1.7.9.5



More information about the weboob mailing list