Mercurial > hg > hybrid-music-recommender-using-content-based-and-social-information
view Code/preview_clip.py @ 13:0a0d6203638a
Working on WMF
author | Paulo Chiliguano <p.e.chiliguano@se14.qmul.ac.uk> |
---|---|
date | Tue, 21 Jul 2015 10:03:34 +0100 |
parents | 20589ba1908a |
children | e68dbee1f6db |
line wrap: on
line source
# -*- coding: utf-8 -*- """ Created on Wed Jul 15 00:41:44 2015 @author: paulochiliguano """ import csv import time from pyechonest import song, config import oauth2 as oauth import urllib2 consumer_key = '7ds28qendsk9' consumer_secret = 'm5nsktn3hu6x45cy' consumer = oauth.Consumer(consumer_key, consumer_secret) config.ECHO_NEST_API_KEY="LINDFDUTQZQ781IE8" with open('/homes/pchilguano/dataset/echonest_songID.txt', 'rb') as input, open('/homes/pchilguano/dataset/7digital_url.txt', 'wb') as output: writer = csv.writer(output, delimiter=',') #for i in xrange(21): #input.readline() next = input.readline() while next != "": try: s = song.Song(next[:-2]) except IndexError: time.sleep(3) next = input.readline() else: time.sleep(3) try: ss_tracks = s.get_tracks('7digital-UK') except: time.sleep(3) next = input.readline() else: #print(len(ss_tracks)) if len(ss_tracks) != 0: ss_track = ss_tracks[0] preview_url = ss_track.get('preview_url') req = oauth.Request(method="GET", url=preview_url, is_form_encoded=True) req['oauth_timestamp'] = oauth.Request.make_timestamp() req['oauth_nonce'] = oauth.Request.make_nonce() req['country'] = "GB" sig_method = oauth.SignatureMethod_HMAC_SHA1() req.sign_request(sig_method, consumer, token=None) try: response = urllib2.urlopen(req.to_url()) except: #time.sleep(16) print([next[:-2], 'NA']) #writer.writerow([next[:-2], 'NA', s.artist_name.encode("utf-8"), s.title.encode("utf-8")]) else: print([next[:-2], preview_url, s.artist_name, s.title]) writer.writerow([next[:-2], preview_url, s.artist_name.encode("utf-8"), s.title.encode("utf-8")]) with open(next[:-2]+'.mp3', 'wb') as songfile: songfile.write(response.read()) time.sleep(16) next = input.readline()