Skip to content

Commit 5fb8a89

Browse files
authored
Merge pull request code-dot-org#32644 from code-dot-org/jan13-spotify
[Applab Datasets] Spotify upload script
2 parents 4f17640 + 7facca5 commit 5fb8a89

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

apps/src/storage/dataBrowser/datasetManifest.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,26 @@
2424
"name": "words",
2525
"description": "Popular words and part of speech.",
2626
"current": false
27+
},
28+
{
29+
"name": "Top 200 USA",
30+
"description": "Top 200 Songs on Spotify for the United States, updated daily.",
31+
"current": true
32+
},
33+
{
34+
"name": "Viral 50 USA",
35+
"description": "Spotify's most viral 50 songs in the United States, updated daily.",
36+
"current": true
37+
},
38+
{
39+
"name": "Top 200 Worldwide",
40+
"description": "The top 200 Songs in the world on Spotify, updated daily.",
41+
"current": true
42+
},
43+
{
44+
"name": "Viral 50 Worldwide",
45+
"description": "Spotify's most viral 50 songs in the world, updated daily.",
46+
"current": true
2747
}
2848
],
2949
"categories": [
@@ -32,6 +52,16 @@
3252
"description": "datasets that are in the shared channel in dev firebase",
3353
"datasets": ["cats", "dogs", "states", "Daily Weather", "words"]
3454
},
55+
{
56+
"name": "Spotify Charts",
57+
"description": "Top songs in the USA and around the world, updated daily. Provided by Spotify at https://spotifycharts.com/.",
58+
"datasets": [
59+
"Top 200 USA",
60+
"Viral 50 USA",
61+
"Top 200 Worldwide",
62+
"Viral 50 Worldwide"
63+
]
64+
},
3565
{
3666
"name": "Animals",
3767
"description": "description",

bin/cron/applab_datasets/spotify

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)