~mmcg069/totallywired/first

« back to all changes in this revision

Viewing changes to totallywired/db/collectors/cdda.py

  • Committer: Matthew McGowan
  • Date: 2012-10-17 20:11:33 UTC
  • Revision ID: matthew.joseph.mcgowan@gmail.com-20121017201133-ygdwxpxcukb3sfqj
dowble click an album cover to play from album.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import os
 
2
import json
2
3
import string
3
4
import xapian
 
5
import urllib
4
6
import urllib2
5
7
import threading
6
8
 
66
68
def get_cdda_artwork(mbid):
67
69
    url = AUDIOSCROBBLER_API + "api_key=%s&method=album.getinfo&mbid=%s&" % (
68
70
        AUDIOSCROBBLER_API_KEY, mbid)
69
 
 
70
 
    print()
71
 
    print("Fetching artwork url from last.fm...")
72
 
    print(url)
73
 
 
74
71
    data = urllib2.urlopen(url, timeout=10)
75
 
    print("Last.fm data received!")
76
72
    root = etree.XML(data.read())
77
73
    image_finder = etree.XPath("//image")
78
74
    images = image_finder(root)
81
77
            im.attrib["size"] == "large"):
82
78
            print("Large image at:", im.text)
83
79
            return im.text
84
 
    print("No url found!")
85
80
    return ""
86
81
 
87
82
def get_cdda_artwork_easily():
88
83
    did = get_cdda_discid()
89
 
    print(did)
90
84
    if not did:
91
85
        return ""
92
86
    mbid = get_cdda_mbid(did)
93
 
    print(mbid)
94
87
    if not mbid:
95
88
        return ""
96
89
    return get_cdda_artwork(mbid)
98
91
# example query
99
92
# query = '"%s" AND artist:"%s"' % (album, artist)
100
93
def get_mbid_from_mbquery(query):
101
 
    print(query)
102
94
    try:
103
95
        if hasattr(mb, "release_search"):
104
96
            res = mb.release_search(query=query, limit=1)
118
110
        album = album[:album.find("[")].strip()
119
111
    elif "(" in album:
120
112
        album = album[:album.find("(")].strip()
121
 
 
122
113
    query = '"%s" AND artist:"%s"' % (album, artist)
123
114
    mbid = get_mbid_from_mbquery(query)
124
115
    if not mbid:
126
117
    return get_cdda_artwork(mbid)
127
118
 
128
119
 
 
120
class SimilarArtist(object):
 
121
    
 
122
    def __init__(self, _dict):
 
123
        self._dict = _dict
 
124
        return
 
125
 
 
126
    def __repr__(self):
 
127
        return self.name
 
128
 
 
129
    @property
 
130
    def name(self):
 
131
        return self._dict["name"]
 
132
 
 
133
    @property
 
134
    def term(self):
 
135
        return self._dict["name"]
 
136
 
 
137
    @property
 
138
    def termfreq(self):
 
139
        return 0
 
140
 
 
141
    @property
 
142
    def image(self):
 
143
        return self._dict["image"][1]["#text"]
 
144
 
 
145
 
 
146
def get_similar(mbid="", artist="", limit=4):
 
147
    limit = limit or 1
 
148
    if mbid:
 
149
        req = "api_key=%s&method=artist.getsimilar&mbid=%s&limit=%s&format=json" % (
 
150
            AUDIOSCROBBLER_API_KEY, mbid, limit)
 
151
    elif artist:
 
152
        req = "api_key=%s&method=artist.getsimilar&artist=%s&autocorrect=1&limit=%s&format=json" % (
 
153
            AUDIOSCROBBLER_API_KEY, urllib.quote(artist), limit)
 
154
    else:
 
155
        raise(ValueError(repr(mbid), repr(artist)))
 
156
    url = AUDIOSCROBBLER_API + req
 
157
    data = urllib2.urlopen(url, timeout=10)
 
158
    return [SimilarArtist(artist) for artist in json.loads(data.read())["similarartists"]["artist"]]
 
159
 
 
160
 
129
161
class CddaCollector(FilenameCollector):
130
162
 
131
163
    def __init__(self, collection):