Demo entry 4094642

Collection

   

Submitted by anonymous on Mar 19, 2016 at 00:09
Language: Python. Code size: 4.1 kB.

# running this could be expensive

import requests
import json
import csv

# collect all my tracks with timestamps
def collectRecent():
  reqParams = {
    'method': 'user.getrecenttracks',
    'user': 'imnoob',
    'api_key': 'db64bf60ac1c59f795994f7325502232',
    'format': 'json',
    'limit': 200
  }

  for pageNum in range(1,518):
    reqParams['page'] = pageNum
    r = requests.get('http://ws.audioscrobbler.com/2.0/', params=reqParams)
    with open('./data/recent_tracks/recent_{0}.json'.format(pageNum), 'w') as outfile:
      json.dump(r.json(), outfile)

# get playcounts of all my artists
def collectTopArtists():
  reqParams = {
    'method': 'user.gettopartists',
    'user': 'imnoob',
    'api_key': 'db64bf60ac1c59f795994f7325502232',
    'format': 'json',
    'limit': 200,
    'period': 'overall'
  }

  for pageNum in range(1,10):
    reqParams['page'] = pageNum
    r = requests.get('http://ws.audioscrobbler.com/2.0/', params=reqParams)
    with open('./data/user_top_artists/user_top_artists_{0}.json'.format(pageNum), 'w') as outfile:
      json.dump(r.json(), outfile)

# get popular tags of artists in my library
def collectArtistInfo():
  reqParams = {
    'method': 'artist.getinfo',
    'username': 'imnoob',
    'api_key': 'db64bf60ac1c59f795994f7325502232',
    'format': 'json'
  }

  with open('./data/user_top_artists.csv', 'rb') as csvFile:
    topArtists = csv.reader(csvFile, delimiter=',')
    topArtists.next() # skip header row
    artistNum = 0
    for row in topArtists:
      reqParams['artist'] = row[0]
      r = requests.get('http://ws.audioscrobbler.com/2.0/', params=reqParams)
      with open('./data/artist_info/artist_info_{0}.json'.format(artistNum), 'w') as outfile:
        json.dump(r.json(), outfile)
      artistNum = artistNum + 1

# get rankings of top artists in every country
def collectArtistsByCountry():
  reqParams = {
    'method': 'geo.gettopartists',
    'api_key': 'db64bf60ac1c59f795994f7325502232',
    'format': 'json',
    'limit': 50,
    'page': 1
  }

  with open('./countries.csv', 'rb') as csvFile:
    countries = csv.reader(csvFile, delimiter=',')
    countries.next() # skip header row
    countryId = 0
    for row in countries:
      country = row[0]
      reqParams['country'] = country
      r = requests.get('http://ws.audioscrobbler.com/2.0/', params=reqParams)
      with open('./data/geo_info/top_artists_country_{0}.json'.format(countryId), 'w') as outfile:
        json.dump(r.json(), outfile)
      countryId = countryId + 1

# for countries with stranger names
def collectArtistsInvalidCountries():
  reqParams = {
    'method': 'geo.gettopartists',
    'api_key': 'db64bf60ac1c59f795994f7325502232',
    'format': 'json',
    'limit': 50,
    'page': 1
  }

  invalidCountries = [
    ("Bolivia", 26),
    ("Macedonia", 131),
    ("Moldova", 145),
    ("Reunion", 180),
    ("Saint Helena", 185),
    ("Taiwan", 217),
    ("Venezuela", 240)
  ]

  for country, countryId in invalidCountries:
    reqParams['country'] = country
    r = requests.get('http://ws.audioscrobbler.com/2.0/', params=reqParams)
    with open('./data/geo_info/top_artists_country_{0}.json'.format(countryId), 'w') as outfile:
      json.dump(r.json(), outfile)

# get artists and playcounts for every week
def collectWeeklyArtists():
  reqParams = {
    'method': 'user.getweeklyartistchart',
    'user': 'imnoob',
    'api_key': 'db64bf60ac1c59f795994f7325502232',
    'format': 'json'
  }

  with open('./data/timestamps.json') as datesFile:
    data = json.load(datesFile)
    timestamps = data['weeklychartlist']['chart']
    for dateRange in timestamps:
      dateFrom = dateRange['from']
      dateTo = dateRange['to']
      reqParams['from'] = dateFrom
      reqParams['to'] = dateTo
      r = requests.get('http://ws.audioscrobbler.com/2.0/', params=reqParams)
      with open('./data/weekly_artist_charts/artist_chart_{0}-{1}.json'.format(dateFrom, dateTo), 'w') as outfile:
        json.dump(r.json(), outfile)

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).