Last active
December 10, 2015 17:58
-
-
Save kristianfreeman/4471077 to your computer and use it in GitHub Desktop.
Wrap the lastexport tool and make nice things with it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'choice' | |
USERNAME = 'kristianfr' | |
# Get some command-line arguments (or one) | |
Choice.options do | |
option :export, :required => true do | |
short '-e' | |
long '--export=EXPORT' | |
desc 'Choose what to export' | |
end | |
end | |
if File.exist?('lastexport.py') | |
puts "Script exists." | |
else | |
puts "Script does not exist, downloading!" | |
`curl -O http://gitorious.org/fmthings/lasttolibre/blobs/raw/master/lastexport.py` | |
end | |
if File.exists?('exported_tracks.txt') | |
puts "Earlier download detected. Processing..." | |
else | |
puts "No data detected." | |
puts "Grab a cup of coffee. This'll take a while." | |
`python lastexport.py --user #{USERNAME}` | |
end | |
data = 'exported_tracks.txt' | |
artist_array = Array.new | |
track_array = Array.new | |
album_array = Array.new | |
artist_hash = Hash.new(0) | |
track_hash = Hash.new(0) | |
album_hash = Hash.new(0) | |
File.readlines(data).each do |t| | |
track = t.scan(/\[\[(.*?)\]\]/)[0] | |
artist = t.scan(/\[\[(.*?)\]\]/)[1] | |
album = t.scan(/\[\[(.*?)\]\]/)[2] | |
artist_array << artist | |
track_array << track | |
album_array << album | |
end | |
def array_to_hash(array, hash) | |
array.each do |k| | |
hash[k] += 1 | |
end | |
end | |
array_to_hash(artist_array, artist_hash) | |
array_to_hash(track_array, track_hash) | |
array_to_hash(album_array, album_hash) | |
sorted_artist = artist_hash.sort_by{|k,v| -v} | |
sorted_tracks = track_hash.sort_by{|k,v| -v} | |
sorted_album = album_hash.sort_by{|k,v| -v} | |
if Choice.choices.export == "artist" | |
File.open('top_artists.txt', 'w') {|f| f.write(sorted_artist) } | |
elsif Choice.choices.export == "album" | |
File.open('top_albums.txt', 'w') {|f| f.write(sorted_albums) } | |
elsif Choice.choices.export == "track" | |
File.open('top_tracks.txt', 'w') {|f| f.write(sorted_tracks) } | |
else | |
puts "Sorry, that didn't work. Try 'artist', 'album', or 'track'" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment