-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScraping_Ultimate.py
168 lines (130 loc) · 5.36 KB
/
Scraping_Ultimate.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 29 01:07:58 2015
@author: Xiaoqian
"""
#initialization of pychonest
from pyechonest import config
from pyechonest import artist,song
import cPickle
import pylast
import urllib
import httplib
import string
import re
import csv
from itertools import islice
from topic_model import *
#initialization of pylast,
api_key='91a81936a4e18b75ad33d62892e36e23'
api_secret='f880ead2f0243c913a050a37c5d33f1d'
username='wings_chains'
password_hash=pylast.md5('Lxq19911116')
config.ECHO_NEST_API_KEY = 'ZIK3QQ0RMMM1TOP2U'
network = pylast.LastFMNetwork(api_key = api_key, api_secret =
api_secret, username = username, password_hash = password_hash)
#test set
#get top 10 tags,
def put_Set(c, val,map):
if not map.__contains__(c):
map[c]=set()
map[c].add(val)
else:
map[c].add(val)
class Track:
def __init__(self,name,artist,album):
self.name = name
self.artist = artist
self.album = album
self.types = []
self.audio_summary={}
def Album_attributes(toread):
alb_songs={}
score_songs={}
#network = pylast.LastFMNetwork(api_key = api_key, api_secret =api_secret, username = username, password_hash = password_hash)
#config.ECHO_NEST_API_KEY = 'ZIK3QQ0RMMM1TOP2U'
with open (toread,'r') as f:
for line in f:
l = line.rstrip('\n')
if l!='"' and l!=None and l!='':
l=l.split(',')
album=l[0]
artist_=l[1]
score=l[4]
#get tags
try:
print artist
alb = network.get_album(artist_,album)
for i in alb.get_tracks():
song_ = i.get_name()
print "***************Track List**************************"
print song_
#add to alb_songs
#collect song features
song_result = song.search(artist=str(artist_),title=str(song_))
if len(song_result)>0:
song_result=song_result[0]
print song_result
#if song_res!=None:
print "*************Song Result************************"
track_ = Track(song_,artist_,album)
track_.types=song_result.song_type
summary = song_result.audio_summary
for k,v in summary.items():
if k!='analysis_url' and k!='audio_md5':
track_.audio_summary[k]=v
track_.audio_summary['hottness']=song_result.get_song_hotttnesss()
track_.audio_summary['currency']=song_result.get_song_currency()
track_.audio_summary['discovery']=song_result.get_song_discovery()
#may need to add artist info
track_.audio_summary['artist_fam']=song_result.artist_familiarity
track_.audio_summary['artist_hot']=song_result.artist_hotttnesss
print len(trsack_.audio_summary)
#add to score_song dictionary (score)
map_putlist(score,track_,score_songs)
map_putlist(album,track_,alb_songs)
except Exception:
pass
saveDict(alb_songs,'alb_songs1')
saveDict(score_songs,'score_songs1')
return alb_songs,score_songs
class Track_tag:
def __init__(self,name,pair):
self.name = name
self.pair=pair
self.tags = []
self.similar = []
self.match=[]
def getTags(song_,lis):
for t in islice(song_.get_top_tags(),150):
lis.append(str(t.item))
def getSimilar(song_,nameList,matchList):
for t in song_.get_similar():
nameList.append(t.item.get_name())
matchList.append(t.match)
#alb_songs,score_songs=Album_attributes('pitchfork_all.csv')
def getTracks(genre_):
album_list={}
track_tag_list={}
for k,v in genre_.items():
for alb in v:
artist_=alb.artist
name_ = alb.name
pair = name_+'-'+artist_
try:
album_ = network.get_album(artist_,name_)
print name_
for i in album_.get_tracks():
song_ = i.get_name()
print song_
#add tracks to the same album (assigned with the same rating)
put_Set(pair,(song_),album_list)
#add tags and find similar ratings
track_tag = Track_tag(song_,pair)
getSimilar(i,track_tag.similar,track_tag.match)
getTags(i,track_tag.tags)
track_tag_list[song_]=track_tag
except Exception:
pass
# print Exception
return album_list,track_tag_list