Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 一小段代码,通过pylast软件从Last.fm API中吸引顶级曲目_Python_Last.fm - Fatal编程技术网

Python 一小段代码,通过pylast软件从Last.fm API中吸引顶级曲目

Python 一小段代码,通过pylast软件从Last.fm API中吸引顶级曲目,python,last.fm,Python,Last.fm,我修改了发布的代码,可以使用Last.fm API提取顶部曲目,如下所示: #!/usr/bin/python import time import pylast import re from md5 import md5 user_name = '*******' user_password = '*******' password_hash = pylast.md5("*******") api_key = '****************************

我修改了发布的代码,可以使用Last.fm API提取顶部曲目,如下所示:

 #!/usr/bin/python

 import time
 import pylast
 import re

 from md5 import md5


 user_name = '*******'
 user_password = '*******'
 password_hash = pylast.md5("*******")
 api_key = '***********************************'
 api_secret = '****************************'
 top_tracks_file = open('top_tracks_wordle.txt', 'w')
 network = pylast.LastFMNetwork(api_key = api_key, api_secret =  api_secret, username =                user_name, password_hash = password_hash)



 # to make the output more interesting for wordle viz.
 # run against all periods. if you just want one period,
 # delete the others from this list
 time_periods = ['PERIOD_12MONTHS', 'PERIOD_6MONTHS', 'PERIOD_3MONTHS', 'PERIOD_OVERALL']
 # time_periods = ['PERIOD_OVERALL']
 #####
 ## shouldn't have to edit anything below here
 #####
 md5_user_password = md5(user_password).hexdigest()

 sg = pylast.SessionKeyGenerator(network) #api_key, api_secret
 session_key = sg.get_session_key(user_name, md5_user_password)

 user = pylast.User(user_name, network) #api_key, api_secret, session_key

 top_tracks = []
 for time_period in time_periods:
     # by default pylast returns a seq in the format:
     #  "Item: Andrew Bird - Fake Palindromes, Weight: 33"
     tracks = user.get_top_tracks(period=time_period)

     # regex that tries to pull out only the track name (
     # for the ex. above "Fake Palindromes"
     p = re.compile('.*[\s]-[\s](.*), Weight: [\d]+')

     for track in tracks:
         m = p.match(str(track))
         **track = m.groups()[0]**                  <-----------Here---------------
         top_tracks.append(track)
     # be nice to last.fm's servers
     time.sleep(5)

 top_tracks = "\n".join(top_tracks)
 top_tracks_file.write(top_tracks)
 top_tracks_file.close()
#/usr/bin/python
导入时间
进口派拉斯特
进口稀土
从md5导入md5
用户名='********'
用户密码='********'
密码\u hash=pylast.md5(“*******”)
api_key='***********************************************'
api_secret='***********************************'
top\u tracks\u file=open('top\u tracks\u wordle.txt','w')
network=pylast.LastFMNetwork(api\u key=api\u key,api\u secret=api\u secret,username=user\u name,password\u hash=password\u hash)
#使wordle viz的输出更有趣。
#与所有周期相反运行。如果你只想要一个周期,
#从这个列表中删除其他的
时段=[“时段12个月”、“时段6个月”、“时段3个月”、“时段整体”]
#时间周期=[“周期整体”]
#####
##不应该在这里编辑下面的任何内容
#####
md5_user_password=md5(user_password).hexdigest()
sg=pylast.SessionKeyGenerator(网络)#api_密钥,api_密钥
会话密钥=sg.获取会话密钥(用户名、md5用户密码)
user=pylast.user(用户名、网络)#api#密钥、api#密钥、会话#密钥
热门曲目=[]
对于时间段中的时间段:
#默认情况下,pylast以以下格式返回一个seq:
#“物品:安德鲁·伯德-假回文,重量:33”
轨道=用户。获取顶部轨道(周期=时间周期)
#试图只提取曲目名称的正则表达式(
#例如,上面的“假回文”
p=re.compile('.[\s]-[\s](.*),权重:[\d]+'))
对于轨道中的轨道:
m=p.匹配(str(轨道))

**track=m.groups()[0]**显然有些曲目名称与正则表达式不匹配,因此match()返回无。捕获异常并检查
track

显然有些曲目名称与正则表达式不匹配,因此match()返回无。捕获异常并检查
track