PYTHON   7

base.py

Guest on 3rd June 2021 05:37:55 AM

  1. import requests
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. from matplotlib import cm
  5. import json
  6. import csv
  7. import unicodecsv as csv
  8. import io
  9. import time
  10.  
  11. lfm_api = '778ac0fc81473b52b8ca6c8c7f476e11'
  12. requests.adapters.DEFAULT_RETRIES = 5
  13.  
  14.  
  15. def clean_str(str):
  16.     return str.encode('utf8')
  17.  
  18.  
  19. def request_lfm(url_rest):
  20.     return do_request('http://ws.audioscrobbler.com/2.0/?method=' + url_rest +
  21.                       '&api_key=' + lfm_api + '&format=json')
  22.  
  23.  
  24. def request_mb(mbid):
  25.     return do_request(
  26.         'http://musicbrainz.org/ws/2/artist/' + mbid + '?inc=aliases&fmt=json',
  27.         False)
  28.  
  29.  
  30. def do_request(url, showUrl=True):
  31.     if showUrl:
  32.         print(url)
  33.     return requests.get(url).json()
  34.  
  35.  
  36. def append_object_to_stored_json(path, key, value):
  37.     loaded_json = json.load(open(path))
  38.     loaded_json[key] = value
  39.     store_json(path, loaded_json)
  40.  
  41.  
  42. def extend_stored_json(path, extension):
  43.     loaded_json = json.load(open(path))
  44.     extended_json = loaded_json.update(extension)
  45.     store_json(path, extended_json)
  46.  
  47.  
  48. def store_json(path, new_json, inform=False):
  49.     with open(path, 'w') as file:
  50.         json.dump(new_json, file)
  51.         if inform:
  52.             print 'file ' + path + ' saved.'
  53.  
  54.  
  55. def clean_json(path):
  56.     store_json(path, {})
  57.  
  58.  
  59. def get_countries(country_pop_limit=200000, country_users_limit=100):
  60.     countries = json.load(open('./data/countries_full.geojson'))
  61.     countries_users = json.load(open('./data/countries_users.json'))
  62.  
  63.     countries_r = []
  64.     for c in countries['features']:
  65.         props = c['properties']
  66.         country_users = False
  67.  
  68.         if props['admin'] in countries_users:
  69.             country_users = countries_users[props['admin']]
  70.         elif props['NAME'] in countries_users:
  71.             country_users = countries_users[props['NAME']]
  72.  
  73.         if country_users and c['properties']['POP_EST'] > country_pop_limit and country_users['sum']['users'] > country_users_limit:
  74.             countries_r.append(c)
  75.  
  76.     return countries_r

Raw Paste


Login or Register to edit or fork this paste. It's free.