Skip to content

Instantly share code, notes, and snippets.

@fofr
Created February 23, 2014 09:50
Show Gist options
  • Save fofr/9169339 to your computer and use it in GitHub Desktop.
Save fofr/9169339 to your computer and use it in GitHub Desktop.
Convert an exported TSV of scrobbles into a SIFTTTER format
#!/usr/bin/ruby
# Convert an exported TSV of scrobbles into a SIFTTTER format
# Export from http://www.last.fm/settings/dataexporter
# SIFTTTER: http://craigeley.com/post/72565974459/sifttter-an-ifttt-to-day-one-logger
require 'time'
require 'erb'
require 'date'
require 'csv'
output_path = 'scrobbles.md'
input_path = 'scrobbles.tsv'
output = ''
entries = []
# http://stackoverflow.com/questions/4404787/whats-the-best-way-to-parse-a-tab-delimited-file-in-ruby
data = CSV.read(input_path, {:col_sep => "\t"})
class Entry
def parse(scrobble)
@ISO_time = scrobble[0]
@unixtime = scrobble[1]
@track_name = scrobble[2]
@track_mbid = scrobble[3]
@artist_name = scrobble[4]
@artist_mbid = scrobble[5]
@uncorrected_track_name = scrobble[6]
@uncorrected_track_mbid = scrobble[7]
@uncorrected_artist_name = scrobble[8]
@uncorrected_artist_mbid = scrobble[9]
@album_name = scrobble[10]
@album_mbid = scrobble[11]
@album_artist_name = scrobble[12]
@album_artist_mbid = scrobble[13]
@application = scrobble[14]
@date = Time.parse(@ISO_time).strftime('%B %d, %Y at %I:%M%p')
@url = "http://www.last.fm/music/#{@artist_name}/_/#{@track_name}".gsub(' ', '+').gsub('(', '%28').gsub(')', '%29')
end
def render
"- #{@date} - \"[#{@track_name}](#{@url})\" by **#{@artist_name}**#{" from *#{@album_name}*" if !@album_name.empty?}#{" via #{@application}" if [email protected]?}"
end
end
data.drop(1).each do |line|
entry = Entry.new
entry.parse(line)
entries << entry
end
fh = File.new(File.expand_path(output_path),'w+')
fh.puts entries.map {|entry| entry.render}
fh.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment