mirror of
https://github.com/rembo10/headphones.git
synced 2026-05-20 02:25:31 +01:00
Some initial last.fm integration
This commit is contained in:
@@ -163,8 +163,31 @@ div.progress-container {
|
||||
float: left;
|
||||
background: white;
|
||||
}
|
||||
a.center {
|
||||
color: blue;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
div.progress-container > div {
|
||||
background-color: #ACE97C;
|
||||
height: 12px
|
||||
}
|
||||
}
|
||||
.cloud{
|
||||
padding: 0px;
|
||||
font-size:16px;
|
||||
}
|
||||
#cloud a.tag1 { font-size: 0.7em; font-weight: 100; }
|
||||
#cloud a.tag2 { font-size: 0.8em; font-weight: 200; }
|
||||
#cloud a.tag3 { font-size: 0.9em; font-weight: 300; }
|
||||
#cloud a.tag4 { font-size: 1.0em; font-weight: 400; }
|
||||
#cloud a.tag5 { font-size: 1.2em; font-weight: 500; }
|
||||
#cloud a.tag6 { font-size: 1.4em; font-weight: 600; }
|
||||
#cloud a.tag7 { font-size: 1.6em; font-weight: 700; }
|
||||
#cloud a.tag8 { font-size: 1.8em; font-weight: 800; }
|
||||
#cloud a.tag9 { font-size: 2.2em; font-weight: 900; }
|
||||
#cloud a.tag10 { font-size: 2.5em; font-weight: 900; }
|
||||
|
||||
#cloud { padding: 2px; line-height: 1.5em; text-align: center; }
|
||||
#cloud a { padding: 0px; }
|
||||
#cloud { margin: 0; }
|
||||
#cloud li { display: inline; }
|
||||
@@ -403,6 +403,7 @@ def dbcheck():
|
||||
c.execute('CREATE TABLE IF NOT EXISTS tracks (ArtistID TEXT, ArtistName TEXT, AlbumTitle TEXT, AlbumASIN TEXT, AlbumID TEXT, TrackTitle TEXT, TrackDuration, TrackID TEXT, TrackNumber INTEGER)')
|
||||
c.execute('CREATE TABLE IF NOT EXISTS snatched (AlbumID TEXT, Title TEXT, Size INTEGER, URL TEXT, DateAdded TEXT, Status TEXT, FolderName TEXT)')
|
||||
c.execute('CREATE TABLE IF NOT EXISTS have (ArtistName TEXT, AlbumTitle TEXT, TrackNumber TEXT, TrackTitle TEXT, TrackLength TEXT, BitRate TEXT, Genre TEXT, Date TEXT, TrackID TEXT)')
|
||||
c.execute('CREATE TABLE IF NOT EXISTS lastfmcloud (ArtistName TEXT, ArtistID TEXT, Count INTEGER)')
|
||||
|
||||
try:
|
||||
c.execute('SELECT IncludeExtras from artists')
|
||||
|
||||
@@ -4,7 +4,7 @@ import os
|
||||
from lib.beets.mediafile import MediaFile
|
||||
|
||||
import headphones
|
||||
from headphones import logger, helpers, db, mb, albumart
|
||||
from headphones import logger, helpers, db, mb, albumart, lastfm
|
||||
|
||||
various_artists_mbid = '89ad4ac3-39f7-470e-963a-56509c546377'
|
||||
|
||||
@@ -130,6 +130,14 @@ def artistlist_to_mbids(artistlist):
|
||||
controlValueDict = {"ArtistID": artistid}
|
||||
newValueDict = {"HaveTracks": havetracks}
|
||||
myDB.upsert("artists", newValueDict, controlValueDict)
|
||||
|
||||
# Update the cloud:
|
||||
logger.info('Updating the cloud')
|
||||
try:
|
||||
lastfm.findSimilar()
|
||||
except Exception, e:
|
||||
logger.warn('Updating the cloud failed: %s' % e)
|
||||
|
||||
|
||||
def addArtisttoDB(artistid, extrasonly=False):
|
||||
|
||||
|
||||
53
headphones/lastfm.py
Normal file
53
headphones/lastfm.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import urllib
|
||||
from xml.dom import minidom
|
||||
from collections import defaultdict
|
||||
import random
|
||||
|
||||
from headphones import db
|
||||
|
||||
api_key = '395e6ec6bb557382fc41fde867bce66f'
|
||||
|
||||
|
||||
def getSimilar():
|
||||
|
||||
myDB = db.DBConnection()
|
||||
results = myDB.select('SELECT ArtistID from artists ORDER BY HaveTracks DESC')
|
||||
|
||||
artistlist = []
|
||||
|
||||
for result in results[:12]:
|
||||
|
||||
url = 'http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&mbid=%s&api_key=%s' % (result['ArtistID'], api_key)
|
||||
data = urllib.urlopen(url).read()
|
||||
d = minidom.parseString(data)
|
||||
node = d.documentElement
|
||||
artists = d.getElementsByTagName("artist")
|
||||
|
||||
for artist in artists:
|
||||
namenode = artist.getElementsByTagName("name")[0].childNodes
|
||||
mbidnode = artist.getElementsByTagName("mbid")[0].childNodes
|
||||
|
||||
for node in namenode:
|
||||
artist_name = node.data
|
||||
for node in mbidnode:
|
||||
artist_mbid = node.data
|
||||
|
||||
if not any(artist_mbid in x for x in results):
|
||||
artistlist.append((artist_name, artist_mbid))
|
||||
|
||||
count = defaultdict(int)
|
||||
|
||||
for artist, mbid in artistlist:
|
||||
count[artist, mbid] += 1
|
||||
|
||||
items = count.items()
|
||||
|
||||
top_list = sorted(items, key=lambda x: x[1], reverse=True)[:25]
|
||||
|
||||
random.shuffle(top_list)
|
||||
|
||||
myDB.action('''DELETE from lastfmcloud''')
|
||||
for tuple in top_list:
|
||||
artist_name, artist_mbid = tuple[0]
|
||||
count = tuple[1]
|
||||
myDB.action('INSERT INTO lastfmcloud VALUES( ?, ?, ?)', [artist_name, artist_mbid, count])
|
||||
@@ -34,6 +34,7 @@ _logobar = '''
|
||||
_nav = '''<div class="nav">
|
||||
<a href="home">HOME</a>
|
||||
<a href="upcoming">UPCOMING</a>
|
||||
<a href="extras">EXTRAS</a>
|
||||
<a href="manage">MANAGE</a>
|
||||
<a href="history">HISTORY</a>
|
||||
<a href="logs">LOGS</a>
|
||||
|
||||
@@ -12,7 +12,7 @@ import threading
|
||||
|
||||
import headphones
|
||||
from headphones.mb import getReleaseGroup
|
||||
from headphones import templates, logger, searcher, db, importer, helpers, mb
|
||||
from headphones import templates, logger, searcher, db, importer, helpers, mb, lastfm
|
||||
from headphones.helpers import checked, radio
|
||||
|
||||
|
||||
@@ -222,11 +222,12 @@ class WebInterface(object):
|
||||
|
||||
artistInfo.exposed = True
|
||||
|
||||
def addArtist(self, artistid):
|
||||
def addArtist(self, artistid, redirect='home'):
|
||||
|
||||
threading.Thread(target=importer.addArtisttoDB, args=[artistid]).start()
|
||||
time.sleep(5)
|
||||
raise cherrypy.HTTPRedirect("home")
|
||||
threading.Thread(target=lastfm.getSimilar).start()
|
||||
raise cherrypy.HTTPRedirect(redirect)
|
||||
|
||||
addArtist.exposed = True
|
||||
|
||||
@@ -649,4 +650,29 @@ class WebInterface(object):
|
||||
page.append(templates._footer % headphones.CURRENT_VERSION)
|
||||
return page
|
||||
|
||||
update.exposed = True
|
||||
update.exposed = True
|
||||
|
||||
def extras(self):
|
||||
myDB = db.DBConnection()
|
||||
cloudlist = myDB.select('SELECT * from lastfmcloud')
|
||||
page = [templates._header]
|
||||
page.append(templates._logobar)
|
||||
page.append(templates._nav)
|
||||
if len(cloudlist):
|
||||
page.append('''
|
||||
<div class="table"><div class="config"><h1>Artists You Might Like:</h1><br /><br />
|
||||
<div class="cloud">
|
||||
<ul id="cloud">''')
|
||||
for item in cloudlist:
|
||||
page.append('<li><a href="addArtist?artistid=%s&redirect=extras" class="tag%i">%s</a></li>' % (item['ArtistID'], item['Count'], item['ArtistName']))
|
||||
page.append('</ul><br /><br /></div></div>')
|
||||
page.append(templates._footer % headphones.CURRENT_VERSION)
|
||||
return page
|
||||
extras.exposed = True
|
||||
|
||||
def updateCloud(self):
|
||||
|
||||
lastfm.getSimilar()
|
||||
raise cherrypy.HTTPRedirect("extras")
|
||||
|
||||
updateCloud.exposed = True
|
||||
Reference in New Issue
Block a user