-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotify_rn.rb
160 lines (127 loc) · 3.99 KB
/
spotify_rn.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# This file is the driver of the application. It gets the information from
# the Spotify API and then sends the actual text message.
# This application uses the RSpotify library. All credit for that goes here:
# https://github.com/guilhermesad/rspotify
# Author: Cody Perry (CPerry26)
require 'rspotify'
require 'twilio-ruby'
class SpotifyRN
# This array contains the different genres to find new releases for.
GENRES = ["rap", "hip-hop", "r&b"]
# These are the client app authorization keys required to make calls to
# the Spotify API.
CLIENT_ID = "YOUR_CLIENT_ID"
CLIENT_SECRET = "YOUR_CLIENT_SECRET"
# These are the keys for the Twilio API.
APP_SID = "TWILIO_SID"
APP_AUTH = "TWILIO_AUTH"
# A list of phone numbers to send notifications to.
# They need to be in the following format:
# [+country code][area code][phone number]
# i.e. "+12225557777"
PHONE_NUMBERS = ["YOUR_PHONE_NUMBERS"]
TWILIO_NUMBER = "YOUR_TWILIO_NUMBER"
def initialize
RSpotify.authenticate(CLIENT_ID, CLIENT_SECRET)
end
# This method gets and returns the newest 50 albums released
# on Spotify for the United States.
#
# Parameters: None
#
# Returns:
# => A list of RSpotify albums (the newest 50 releases in the US).
def get_new_releases
return RSpotify::Album.new_releases(country: 'US', limit: 50)
end
# This method filters the full results for the releases in the desired
# GENRES and released on the current day. The idea is this application
# would be run every Friday on the release day of Spotify.
#
# Parameters:
# => releases : A list of releases to filter.
#
# Returns:
# => An array of filtered results.
def filter_releases(releases)
filteredResults = []
releases.each { |release|
release.genres.each { |genre|
if release.release_date.eql? Time.now.strftime("%Y-%m-%d")
filteredResults << release
end
}
}
return filteredResults
end
# This function builds the text message text which gets sent the phone
# numbers.
#
# Parameters:
# => releases : A set of releases to be sent to the phone numbers.
#
# Returns:
# => A string with the text to send.
def build_text_string(releases)
albums = "Albums:\n"
singles = "Singles:\n"
compilations = "Compilations:\n"
releases.each { |release|
if release.album_type.eql? "album"
albums << "- " << release.name << " : "
release.artists.each { |artist|
albums << artist.name
albums << ", " if release.artists.last != artist
}
albums << "\n"
elsif release.album_type.eql? "single"
singles << "- " << release.name << " : "
release.artists.each { |artist|
singles << artist.name
singles << ", " if release.artists.last != artist
}
singles << "\n"
else
compilations << "- " << release.name << " : "
release.artists.each { |artist|
compilations << artist.name
compilations << ", " if release.artists.last != artist
}
compilations << "\n"
end
}
# Add "None" if no results were found for that category.
albums << "None\n" if albums.length <= 8
singles << "None\n" if singles.length <= 9
compilations << "None\n" if compilations.length <= 14
return albums + "\n" + singles + "\n" + compilations
end
# This method sends the constructed text message to each phone number
# in the PHONE_NUMBERS array.
#
# Parameters:
# => textMessage : The text message to send.
#
# Returns: None
def send_text_message(textMessage)
client = Twilio::REST::Client.new(APP_SID, APP_AUTH)
PHONE_NUMBERS.each { |number|
begin
client.messages.create(
from: TWILIO_NUMBER,
to: number,
body: textMessage
)
rescue Exception => e
print("Could not send text message! See below:\n" + e.to_s + "\n")
return
end
print("Text message sent to " + number + " successfully!")
}
end
end
spotifyRN = SpotifyRN.new
newReleases = spotifyRN.get_new_releases
filteredResults = spotifyRN.filter_releases(newReleases)
textBody = spotifyRN.build_text_string(filteredResults)
spotifyRN.send_text_message(textBody)