librefm-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Librefm-commits] [1739] Merge scripts/ from trunk


From: Clint Adams
Subject: [Librefm-commits] [1739] Merge scripts/ from trunk
Date: Sat, 23 May 2009 16:04:57 +0000

Revision: 1739
          http://svn.sv.gnu.org/viewvc/?view=rev&root=librefm&revision=1739
Author:   clint
Date:     2009-05-23 16:04:56 +0000 (Sat, 23 May 2009)
Log Message:
-----------
Merge scripts/ from trunk

Modified Paths:
--------------
    branches/stable/scripts/gobble.py
    branches/stable/scripts/import-jamendo.py
    branches/stable/scripts/most_recent.py
    branches/stable/scripts/offline-submit.py

Added Paths:
-----------
    branches/stable/scripts/users_dump_rdf.php

Modified: branches/stable/scripts/gobble.py
===================================================================
--- branches/stable/scripts/gobble.py   2009-05-22 16:25:09 UTC (rev 1738)
+++ branches/stable/scripts/gobble.py   2009-05-23 16:04:56 UTC (rev 1739)
@@ -4,6 +4,7 @@
 except ImportError:
     import md5
     md5hash = md5.new
+from optparse import OptionParser
 import time
 from urllib import urlencode
 from urllib2 import urlopen
@@ -16,9 +17,10 @@
 
 class GobbleServer(object):
 
-    def __init__(self, server_name, username, password):
+    def __init__(self, server_name, username, password, client_code='imp'):
         if server_name[:7] != "http://":
             server_name = "http://%s"; % (server_name,)
+        self.client_code = client_code
         self.name = server_name
         self.password = password
         self.post_data = []
@@ -32,9 +34,11 @@
         timestamp = int(time.time())
         token = (md5hash(md5hash(self.password).hexdigest()
                     + str(timestamp)).hexdigest())
-        auth_url = "%s/?hs=true&p=1.2&u=%s&t=%d&a=%s&c=imp" % (self.name,
-                                                               self.username,
-                                                               timestamp, 
token)
+        auth_url = "%s/?hs=true&p=1.2&u=%s&t=%d&a=%s&c=%s" % (self.name,
+                                                              self.username,
+                                                              timestamp,
+                                                              token,
+                                                              self.client_code)
         response = urlopen(auth_url).read()
         lines = response.split("\n")
         if lines[0] != "OK":
@@ -91,3 +95,12 @@
         if self.mbid is not None:
             data.append(('m[%d]' % i, self.mbid))
         return data
+
+
+def get_parser(usage):
+    parser = OptionParser(usage=usage)
+    parser.add_option('-s', '--server',
+                      help="Server to submit to.  Defaults to"
+                           " 'turtle.libre.fm'.")
+    parser.set_defaults(server='turtle.libre.fm')
+    return parser

Modified: branches/stable/scripts/import-jamendo.py
===================================================================
--- branches/stable/scripts/import-jamendo.py   2009-05-22 16:25:09 UTC (rev 
1738)
+++ branches/stable/scripts/import-jamendo.py   2009-05-23 16:04:56 UTC (rev 
1739)
@@ -152,14 +152,14 @@
                                                self.conn.commit()
                                        except Exception,  e:
                                                self.conn.rollback()
-                                               print e
+                                               print 'ua', e
                                else:
                                        try:
-                                               self.cursor.execute("INSERT 
INTO Artist (name, image_small, mbid, homepage)  VALUES (%s, %s, %s, %s)", 
(artist["name"], artist["image"], artist["url"], artist["mbid"]))
+                                               self.cursor.execute("INSERT 
INTO Artist (name, image_small, mbid, homepage)  VALUES (%s, %s, %s, %s)", 
(artist["name"], artist["image"], artist["mbid"], artist["url"]))
                                                self.conn.commit()
                                        except Exception,  e:
                                                self.conn.rollback()
-                                               print e
+                                               print 'ia', e
 
                                for album in artist["albums"]:
                                        if self.album_exists(artist["name"], 
album["name"]):
@@ -170,7 +170,7 @@
                                                        self.conn.commit()
                                                except Exception,  e:
                                                        self.conn.rollback()
-                                                       print e
+                                                       print 'ub', e
                                        else:
                                                try:
                                                        
self.cursor.execute("INSERT INTO Album (name, artist_name, albumurl, image, 
artwork_license, mbid, releasedate, downloadurl) VALUES (%s, %s, %s, %s, %s, 
%s, %s, %s)",
@@ -178,7 +178,7 @@
                                                        self.conn.commit()
                                                except Exception,  e:
                                                        self.conn.rollback()
-                                                       print e
+                                                       print 'ib', e
 
                                        for tag in album["tags"]:
                                                if not self.tag_exists(tag, 
artist["name"], album["name"]):
@@ -188,7 +188,7 @@
                                                                
self.conn.commit()
                                                        except Exception,  e:
                                                                
self.conn.rollback()
-                                                               print e
+                                                               print 'ig', e
 
                                        for track in album["tracks"]:
 
@@ -211,18 +211,18 @@
 
                                                if 
self.track_exists(artist["name"], album["name"], track["name"]):
                                                        try:
-                                                               
self.cursor.execute("UPDATE Track SET downloadurl = %s, streamurl = %s, mbid = 
%s, license = %s, duration = %s, otherid = %s, streamable = %s WHERE name = %s 
AND artist = %s AND album = %s", (track["downloadurl"], track["streamurl"], 
track["mbid"], track["license"], duration, otherid, streamable, track["name"], 
artist["name"], album["name"]))
+                                                               
self.cursor.execute("UPDATE Track SET downloadurl = %s, streamurl = %s, mbid = 
%s, license = %s, duration = %s, otherid = %s, streamable = %s WHERE name = %s 
AND artist_name = %s AND album_name = %s", (track["downloadurl"], 
track["streamurl"], track["mbid"], track["license"], duration, otherid, 
streamable, track["name"], artist["name"], album["name"]))
                                                                
self.conn.commit()
                                                        except Exception,  e:
                                                                
self.conn.rollback()
-                                                               print e
+                                                               print 'ut', e
                                                else:
                                                        try:
-                                                               
self.cursor.execute("INSERT INTO Track (name, artist, album, mbid, downloadurl, 
streamurl, license, duration, otherid, streamable) VALUES (%s, %s, %s, %s, %s, 
%s, %s, %s, %s, %s)", (track["name"], artist["name"], album["name"], 
track["mbid"], track["downloadurl"], track["streamurl"], track["license"], 
duration, otherid, streamable))
+                                                               
self.cursor.execute("INSERT INTO Track (name, artist_name, album_name, mbid, 
downloadurl, streamurl, license, duration, otherid, streamable) VALUES (%s, %s, 
%s, %s, %s, %s, %s, %s, %s, %s)", (track["name"], artist["name"], 
album["name"], track["mbid"], track["downloadurl"], track["streamurl"], 
track["license"], duration, otherid, streamable))
                                                                
self.conn.commit()
                                                        except Exception,  e:
                                                                
self.conn.rollback()
-                                                               print e
+                                                               print 'it', e
 
                                                for tag in track["tags"]:
                                                        if not 
self.tag_exists(tag, artist["name"], album["name"], track["name"]):
@@ -232,7 +232,7 @@
                                                                        
self.conn.commit()
                                                                except 
Exception,  e:
                                                                        
self.conn.rollback()
-                                                                       print e
+                                                                       print 
'ig2', e
 
 
 
@@ -261,7 +261,10 @@
                                artist["image"] = artist_e.text
                        
                        if artist_e.tag == "mbgid":
-                               artist["mbid"] = artist_e.text
+                               if artist_e.text == None or len(artist_e.text) 
== 36:
+                                       artist["mbid"] = artist_e.text
+                               else:
+                                       print "Artist mbgid wrong length (%d): 
%s" % (len(artist_e.text),artist_e.text)
 
                        if artist_e.tag == "url":
                                artist["url"] = artist_e.text
@@ -296,7 +299,10 @@
                                album["tags"].append(genre)
                
                        if album_e.tag == "mbgid":
-                               album["mbid"] = album_e.text
+                               if album_e.text == None or len(album_e.text) == 
36:
+                                       album["mbid"] = album_e.text
+                               else:
+                                       print "Album mbgid wrong length (%d): 
%s" % (len(album_e.text),album_e.text)
                        
                        if album_e.tag == "license_artwork":
                                album["license_artwork"] = album_e.text
@@ -336,6 +342,12 @@
                        if track_e.tag == "duration":
                                track["duration"] = track_e.text
 
+                       if track_e.tag == "mbgid":
+                               if track_e.text == None or len(track_e.text) == 
36:
+                                       track["mbid"] = track_e.text
+                               else:
+                                       print "Track mbgid wrong length (%d): 
%s" % (len(track_e.text),track_e.text)
+
                        if track_e.tag == "Tags":
                                for tag_e in track_e.getchildren():
                                        
track["tags"].append(self.proc_tag(tag_e))
@@ -365,7 +377,7 @@
 
        def track_exists(self, artist, album, track):
                try:
-                       self.cursor.execute("SELECT name FROM Track WHERE 
artist = %s AND album = %s AND name = %s", (artist, album, track))
+                       self.cursor.execute("SELECT name FROM Track WHERE 
artist_name = %s AND album_name = %s AND name = %s", (artist, album, track))
                        return self.cursor.rowcount != 0
                except:
                        return False
@@ -373,7 +385,10 @@
 
        def tag_exists(self, tag, artist, album, track=None):
                try:
-                       self.cursor.execute("SELECT tag FROM Tags WHERE tag = 
%s AND artist = %s AND album = %s AND track = %s", (tag, artist, album, track))
+                       if track:
+                               self.cursor.execute("SELECT tag FROM Tags WHERE 
tag = %s AND artist = %s AND album = %s AND track = %s", (tag, artist, album, 
track))
+                       else:
+                               self.cursor.execute("SELECT tag FROM Tags WHERE 
tag = %s AND artist = %s AND album = %s AND track = ''", (tag, artist, album))
                        return self.cursor.rowcount != 0
                except:
                        return False

Modified: branches/stable/scripts/most_recent.py
===================================================================
--- branches/stable/scripts/most_recent.py      2009-05-22 16:25:09 UTC (rev 
1738)
+++ branches/stable/scripts/most_recent.py      2009-05-23 16:04:56 UTC (rev 
1739)
@@ -5,16 +5,19 @@
 from urllib2 import urlopen
 
 from BeautifulSoup import BeautifulSoup
+from gobble import get_parser
 
 
 if __name__ == '__main__':
-    usage = "%prog <USERNAME> <WEB SERVER>"
-    parser = OptionParser(usage=usage)
+    usage = "%prog [-s <WEB SERVER>] <USERNAME>"
+    parser = get_parser(usage=usage)
+    parser.set_defaults(server='alpha.libre.fm')
     opts,args = parser.parse_args()
-    if len(args) != 2:
+    if len(args) != 1:
         parser.error("All arguments are required.")
 
-    username,server = args
+    username, = args
+    server = opts.server
     if server[:7] != "http://":
         server = "http://%s"; % (server,)
     soup = BeautifulSoup(urlopen('%s/user/%s/recent-tracks'

Modified: branches/stable/scripts/offline-submit.py
===================================================================
--- branches/stable/scripts/offline-submit.py   2009-05-22 16:25:09 UTC (rev 
1738)
+++ branches/stable/scripts/offline-submit.py   2009-05-23 16:04:56 UTC (rev 
1739)
@@ -4,11 +4,12 @@
 import getpass
 from optparse import OptionParser
 import subprocess
+import sys
 
 import mutagen
 from mutagen import easyid3
 
-from gobble import GobbleServer, GobbleTrack
+from gobble import get_parser, GobbleServer, GobbleTrack
 
 
 def _parse_date(string):
@@ -18,26 +19,71 @@
     return datetime.datetime.utcfromtimestamp(float(string))
 
 
+def _date_prompt(dt):
+    input = ''
+    while input not in ['y', 'n']:
+        input = raw_input("Did you mean '%s UTC'? [Y/n]: " % (dt,)).lower()
+    if input == 'n':
+        sys.exit()
+
+
+def _get_date_from_string(start_string):
+    dt = _parse_date(start_string)
+    _date_prompt(dt)
+    return dt
+
+
+def _get_offset_date(tracks):
+    offset = datetime.timedelta()
+    for track in tracks:
+        offset += datetime.timedelta(seconds=_get_track(track).info.length)
+    dt = datetime.datetime.now() - offset
+    _date_prompt(dt)
+    return dt
+
+
+def _get_track(filename):
+    f = mutagen.File(filename)
+    if f is None:
+        raise Exception("%s caused problems." % (track,))
+    if isinstance(f, mutagen.mp3.MP3):
+        f = mutagen.mp3.MP3(filename, ID3=easyid3.EasyID3)
+    return f
+
+
 if __name__ == '__main__':
-    usage = "%prog <USERNAME> <SERVER> <START TIME> <MEDIA FILES>"
-    parser = OptionParser(usage=usage)
+    usage = "%prog [--server <SERVER>] <USERNAME> <START TIME> <MEDIA FILES>"
+    parser = get_parser(usage=usage)
+    parser.add_option('-j', '--just-finished', action="store_true",
+                      help="Works out START TIME as if you've just finished"
+                           " listening to MEDIA FILES.  START TIME argument"
+                           " will be treated as a media file, so don't pass"
+                           " it.")
+    parser.set_defaults(just_finished=False)
     opts,args = parser.parse_args()
-    if len(args) < 4:
+    if opts.just_finished:
+        expected_args = 2
+    else:
+        expected_args = 3
+    if len(args) < expected_args:
         parser.error("All arguments are required.")
 
-    username,server,start_string = args[:3]
+    username = args.pop(0)
+    start_string = None
+    if not opts.just_finished:
+        start_string = args.pop(0)
+    server = opts.server
     password = getpass.getpass()
-    tracks = args[3:]
+    tracks = args
     server = GobbleServer(server, username, password)
 
-    dt = _parse_date(start_string)
+    if opts.just_finished:
+        dt = _get_offset_date(tracks)
+    else:
+        dt = _get_date_from_string(start_string)
 
     for track in tracks:
-        f = mutagen.File(track)
-        if f is None:
-            raise Exception("%s caused problems." % (track,))
-        if isinstance(f, mutagen.mp3.MP3):
-            f = mutagen.mp3.MP3(track, ID3=easyid3.EasyID3)
+        f = _get_track(track)
         title = f['title'][0]
         artist = f['artist'][0]
         length = f.info.length

Added: branches/stable/scripts/users_dump_rdf.php
===================================================================
--- branches/stable/scripts/users_dump_rdf.php                          (rev 0)
+++ branches/stable/scripts/users_dump_rdf.php  2009-05-23 16:04:56 UTC (rev 
1739)
@@ -0,0 +1,64 @@
+<?php
+
+include '../nixtape/config.php';
+include '../nixtape/database.php';
+include '../nixtape/utils/linkeddata.php';
+
+function htmlnumericentities($str)
+{ 
+       return preg_replace('/[^!-%\x27-;=?-~ ]/e', '"&#".ord("$0").chr(59)', 
$str); 
+}
+
+$username = $argv[1];
+if (! $username)
+{
+       die("Must provide a username.\n");
+}
+
+$user = new User($username);
+$scrobbles = $user->getScrobbles(10000);
+
+print "<rdf:RDF
+       xmlns=\"http://purl.org/ontology/mo/\";
+       xmlns:bio=\"http://purl.org/vocab/bio/0.1/\";
+       xmlns:dc=\"http://purl.org/dc/terms/\";
+       xmlns:foaf=\"http://xmlns.com/foaf/0.1/\";
+       xmlns:gob=\"http://purl.org/ontology/last-fm/\";
+       xmlns:mo=\"http://purl.org/ontology/mo/\";
+       xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\";
+       xmlns:sioc=\"http://rdfs.org/sioc/ns#\";
+       xmlns:rss=\"http://purl.org/rss/1.0/\";
+       xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\";
+       xmlns:geo=\"http://www.w3.org/2003/01/geo/wgs84_pos#\";
+       xml:lang=\"en\">\n";
+
+while ($s = array_shift($scrobbles))
+{
+       printf("
+       <gob:ScrobbleEvent rdf:about=\"%s\">
+               <gob:user rdf:resource=\"%s\" />
+               <gob:track_played>
+                       <Track rdf:about=\"%s\" dc:title=\"%s\">
+                               <foaf:maker>
+                                       <MusicArtist rdf:about=\"%s\" 
foaf:name=\"%s\" />
+                               </foaf:maker>
+                       </Track>
+               </gob:track_played>
+               <dc:date 
rdf:datatype=\"http://www.w3.org/2001/XMLSchema#dateTime\";>%s</dc:date>
+       </gob:ScrobbleEvent>
+       <Record about=\"%s\">
+               <track rdf:resource=\"%s\" />
+       </Record>\n",
+       htmlnumericentities($s['id']),
+       htmlnumericentities($user->id),
+       htmlnumericentities($s['id_track']),
+       htmlnumericentities($s['track']),
+       htmlnumericentities($s['id_artist']),
+       htmlnumericentities($s['artist']),
+       htmlnumericentities($s['timeiso']),
+       htmlnumericentities($s['id_album']),
+       htmlnumericentities($s['id_track'])
+       );
+}
+
+print "</rdf:RDF>\n";
\ No newline at end of file





reply via email to

[Prev in Thread] Current Thread [Next in Thread]