Skip to content

Commit

Permalink
fix: update search query, add Dockerfile and compose
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Duss <[email protected]>
  • Loading branch information
andrewduss committed Jun 6, 2024
1 parent a7b02b2 commit 30620a3
Show file tree
Hide file tree
Showing 9 changed files with 1,596 additions and 514 deletions.
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM ruby:2.4

WORKDIR /code
RUN gem install bundler --version 2.3.27
COPY Gemfile Gemfile.lock .
RUN bundle install

COPY . .
5 changes: 4 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
GEM
specs:

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -71,4 +74,4 @@ DEPENDENCIES
sinatra!

BUNDLED WITH
2.0.2
2.3.27
7 changes: 4 additions & 3 deletions add_from_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

ENV["access_token"] = cli_refresh_token()

File.readlines('songs.txt').each do |line|
puts("[*] Adding song #{line}")
File.readlines('songs.txt').each_with_index do |line, index|
puts("[*] Adding song #{index} #{line}")
index = index + 781 # Start at the end of the playlist
uri = get_track_uri(line)
add_track(uri)
add_track(uri, index)
end

4 changes: 4 additions & 0 deletions conf/configure.rb.sample
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ def configure_env
ENV["client_secret"] ||= "<Your Spotify Client Secret>"
ENV["playlist_id"] ||= "<Playlist ID>"

# ENV["playlist_id"] ||= "1VJVFypnr5RFbUvRIEF6Pu" # Song of the Day
# ENV["playlist_id"] ||= "4rGQPqMNfnG6O0T3xmWlkw" # Runner Powered Podcast
# ENV["playlist_id"] ||= "3GTRVTO7OlxPUFzH5AOqj4" # Auto KEXP RPP

# If using CLI, add refresh token obtained via other instance of app
ENV["cli_refresh_token"] = "<Refresh Token>"
end
6 changes: 6 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
sotd:
build: .
volumes:
- .:/code
command: "sleep 99999"
7 changes: 5 additions & 2 deletions lib/add-track.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Add track to Spotify Playlist via Track URI
# Returns FALSE if unsuccessful
def add_track(track_uri)
def add_track(track_uri, position=0)
require 'net/http'
require 'uri'
require_relative '../conf/configure'
Expand All @@ -10,7 +10,7 @@ def add_track(track_uri)
playlist_id = ENV["playlist_id"]
token = ENV["access_token"]

uri = URI.parse("https://api.spotify.com/v1/users/andyduss/playlists/" + playlist_id.to_s + "/tracks?position=0&uris=" + track_uri.to_s)
uri = URI.parse("https://api.spotify.com/v1/users/andyduss/playlists/" + playlist_id.to_s + "/tracks?position=" + position.to_s + "&uris=" + track_uri.to_s)
request = Net::HTTP::Post.new(uri)
request["Accept"] = "application/json"
request["Authorization"] = "Bearer " + token
Expand All @@ -29,6 +29,9 @@ def add_track(track_uri)
if response.code == "201"
puts "Successfully added track to playlist"
return true
elsif response.code == "200"
puts "Successfully added track to playlist"
return true
elsif response.code == "400"
# do nothing, user already alerted in 'get-track-uri'
else
Expand Down
8 changes: 5 additions & 3 deletions lib/get-track-uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ def get_track_uri(search_query)

# Converts UTF-8 Chars into ASCII for URI Search
I18n.available_locales = [:en]
query = I18n.transliterate(search_query.strip.gsub(/\(feat.+.*/,'').gsub(' ','+').gsub('&','')) #change search_query spaces to '+'

# puts "Query: #{query}" # Debugging
# query = I18n.transliterate(search_query.strip.gsub(/\(feat.+.*/,'').gsub(' ','+').gsub('&','')) #change search_query spaces to '+'
# query = I18n.transliterate(search_query.strip.gsub(/\(feat.+.*\)/,'').gsub(' ','+').gsub('&','')) # Change to only remove (feat. ... )
query = I18n.transliterate(search_query.strip.gsub('(','').gsub(')','').gsub(' ','+').gsub('&','')) # Change to keep "feat." but remove parenthesis
puts "Query: #{query}" # Debugging

# query Spotify Web API
response = RestClient.get 'https://api.spotify.com/v1/search?q=' + query + '&type=track', {Accept: 'application/json', 'Authorization' => 'Bearer ' + ENV["access_token"]}
Expand All @@ -28,6 +29,7 @@ def get_track_uri(search_query)
resp = JSON.parse(response.body)
resp = resp['tracks'].to_s.split(" ")
track_uri = String.new

resp.each do |line|
if line.include?('uri"=>"spotify:track:')
track_uri = line.match(/spotify:track:([\d\w]+)/)[0] # Grab the Track ID
Expand Down
Loading

0 comments on commit 30620a3

Please sign in to comment.