|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +# This script fetches spotify data from api.spotify and uploads to the shared firebase channel. |
| 4 | +require_relative '../../../deployment' |
| 5 | +require 'cdo/only_one' |
| 6 | +require 'net/http' |
| 7 | +require 'csv' |
| 8 | +require 'ostruct' |
| 9 | +require_relative '../../../shared/middleware/helpers/firebase_helper' |
| 10 | + |
| 11 | +TOP_200_US_URL = 'https://spotifycharts.com/regional/us/daily/latest/download' |
| 12 | +TOP_200_WORLD_URL = 'https://spotifycharts.com/regional/global/daily/latest/download' |
| 13 | +VIRAL_50_US_URL = 'https://spotifycharts.com/viral/us/daily/latest/download' |
| 14 | +VIRAL_50_WORLD_URL = 'https://spotifycharts.com/viral/global/daily/latest/download' |
| 15 | + |
| 16 | +def get_top200_data(url) |
| 17 | + records = {} |
| 18 | + columns = ['Position', 'Track Name', 'Artist', 'Streams', 'URL'] |
| 19 | + response = Net::HTTP.get_response(URI(url)) |
| 20 | + return unless response.code == '200' |
| 21 | + id = 1 |
| 22 | + response.body.force_encoding('utf-8') |
| 23 | + csv = CSV.new(response.body) |
| 24 | + lines = csv.read |
| 25 | + lines.shift # first line just contains a note |
| 26 | + lines.shift # second line is column names |
| 27 | + lines.each do |line| |
| 28 | + record = OpenStruct.new |
| 29 | + record.id = id |
| 30 | + record.Position = line[0].to_i |
| 31 | + record['Track Name'] = line[1] |
| 32 | + record.Artist = line[2] |
| 33 | + record.Streams = line[3].to_i |
| 34 | + record.URL = line[4] |
| 35 | + |
| 36 | + records[id] = record.to_h.to_json |
| 37 | + id += 1 |
| 38 | + end |
| 39 | + return records, columns |
| 40 | +end |
| 41 | + |
| 42 | +def get_viral50_data(url) |
| 43 | + records = {} |
| 44 | + columns = ['Position', 'Track Name', 'Artist', 'URL'] |
| 45 | + response = Net::HTTP.get_response(URI(url)) |
| 46 | + return unless response.code == '200' |
| 47 | + id = 1 |
| 48 | + response.body.force_encoding('utf-8') |
| 49 | + csv = CSV.new(response.body) |
| 50 | + lines = csv.read |
| 51 | + lines.shift # first line is column names |
| 52 | + lines.each do |line| |
| 53 | + record = OpenStruct.new |
| 54 | + record.id = id |
| 55 | + record.Position = line[0].to_i |
| 56 | + record['Track Name'] = line[1] |
| 57 | + record.Artist = line[2] |
| 58 | + record.URL = line[3] |
| 59 | + |
| 60 | + records[id] = record.to_h.to_json |
| 61 | + id += 1 |
| 62 | + end |
| 63 | + return records, columns |
| 64 | +end |
| 65 | + |
| 66 | +def main |
| 67 | + fb = FirebaseHelper.new('shared') |
| 68 | + records, columns = get_top200_data(TOP_200_US_URL) |
| 69 | + fb.delete_shared_table('Top%20200%20USA') |
| 70 | + fb.upload_shared_table('Top%20200%20USA', records, columns) |
| 71 | + |
| 72 | + records, columns = get_top200_data(TOP_200_WORLD_URL) |
| 73 | + fb.delete_shared_table('Top%20200%20Worldwide') |
| 74 | + fb.upload_shared_table('Top%20200%20Worldwide', records, columns) |
| 75 | + |
| 76 | + records, columns = get_viral50_data(VIRAL_50_US_URL) |
| 77 | + fb.delete_shared_table('Viral%2050%20USA') |
| 78 | + fb.upload_shared_table('Viral%2050%20USA', records, columns) |
| 79 | + |
| 80 | + records, columns = get_viral50_data(VIRAL_50_WORLD_URL) |
| 81 | + fb.delete_shared_table('Viral%2050%20Worldwide') |
| 82 | + fb.upload_shared_table('Viral%2050%20Worldwide', records, columns) |
| 83 | +end |
| 84 | + |
| 85 | +main if only_one_running?(__FILE__) |
0 commit comments