-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlastfm.py
58 lines (53 loc) · 1.81 KB
/
lastfm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- coding: utf-8 -*-
# http://d.hatena.ne.jp/ninoseki/20091018/1255866133
import urllib2
import re
import os
import io
from BeautifulSoup import BeautifulStoneSoup
from PIL import Image
class AlbumArt:
def __init__(self, artist, album, api_key):
self.api_key = api_key
self.artist = artist
self.album = album
pass
def openUrl(self):
artist = re.sub(" ", "%20", self.artist)
album = re.sub(" ", "%20", self.album)
url = "http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=%s&artist=%s&album=%s" % (self.api_key, artist, album)
try:
xml = urllib2.urlopen(url).read()
pass
except Exception:
# print "can't open url"
return 0
return xml
def getImageUrl(self, xml):
image_url = ""
soup = BeautifulStoneSoup(xml)
e = soup.find("lfm")
if e.has_key("status") and e["status"] == "ok":
images = e.findAll("image")
if images[1].contents:
#images[ここの数字を大きくすると画像サイズも大きくなる]
image_url = images[3].contents[0]
pass
pass
if image_url:
return image_url
else:
return 0
pass
def saveImage(self, image_url):
stream = urllib2.urlopen(image_url)
if stream.info().gettype()[:5] == "image":
python_script_path = os.path.abspath(os.path.dirname(__file__))
file_name = python_script_path + "/.img/tmp.jpg"
image = Image.open(io.BytesIO(stream.read())).convert('RGB')
image.save(file_name, quality = 100)
pass
else:
return 0
# print "success! AlbumArt saved as %s" % file_name
stream.close()