Class: Lily

Inherits:
Object
  • Object
show all
Defined in:
lib/lily.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Lily

Returns a new instance of Lily.



10
11
12
13
14
15
16
17
18
# File 'lib/lily.rb', line 10

def initialize(*args)
  @args = args
  @keys = {
    "public": "238b1080c5a14212261f2c963baaf2db",
    "private": "85fbaa6644c31d879c3e004fbddb82bf"
  }
  @lily_json = self.get_lily_json
  @token ||= self.token
end

Instance Method Details

#authObject



55
56
57
58
59
# File 'lib/lily.rb', line 55

def auth
  lastfmURL = "http://www.last.fm/api/auth/?api_key=#{@keys.public}&token=#{@token}"
  puts "Press Enter once you've given access."
  Launchy.open(lastfmURL)
end

#format_params(params) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/lily.rb', line 31

def format_params(params)
  path_arr = []
  sig_str = ""
  params = params.sort_by { |key, value| key }
  params.each do |key, value|
    path_arr.push("#{key}=" + URI.encode(value.to_s))
    if (key != "format")
      sig_str += key + value.to_s
    end
  end
  sig_str += @keys.private
  sig = Digest::MD5.hexdigest(sig_str)
  path_arr.push("api_sig=#{sig}")
  path = path_arr.join("&")
  return path
end

#get_lily_jsonObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/lily.rb', line 20

def get_lily_json
  begin # Make this more concise later
    content = File.read(ENV['HOME'] + '/.lily.json')
    lily_json = JSON.parse(content)

    # returns {sk, signature}
  rescue
    puts "~/.lily.json appears empty"
  end
end

#helpObject



104
105
106
# File 'lib/lily.rb', line 104

def help
  puts "no"
end

#initObject



108
109
110
111
112
113
# File 'lib/lily.rb', line 108

def init
  auth
  STDIN.gets # wow, hack
  session_key
  puts "Initialized successfully"
end

#runObject



94
95
96
97
98
99
100
101
102
# File 'lib/lily.rb', line 94

def run
  first = @args.shift

  case first
  when 'scrobble' then self.scrobble
  when 'init' then self.init
  else self.help
  end
end

#scrobbleObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/lily.rb', line 72

def scrobble
  # http://www.last.fm/api/show/track.scrobble
  track, artist = @args[0], @args[1]

  url = "http://ws.audioscrobbler.com/2.0/"
  timestamp = Time.now.utc.to_i # UTC time since unix epoch in seconds

  body = self.format_params({
    "artist" => artist,
    "method" => "track.scrobble",
    "sk" => @lily_json.session_key,
    "timestamp" => timestamp,
    "track" => track,
    "api_key" => @keys.public,
    "format" => "json"
  })

  res = HTTP.post(url, :body => body)

  puts "#{track} by #{artist} has been scrobbled"
end

#session_keyObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/lily.rb', line 61

def session_key
  sig = Digest::MD5.hexdigest("api_key#{@keys.public}methodauth.getSessiontoken#{@token}#{@keys.private}")
  session = "http://ws.audioscrobbler.com/2.0/?method=auth.getSession&api_key=#{@keys.public}&api_sig=#{sig}&token=#{@token}&format=json"
  res = JSON.parse(HTTP.get(session))
  sk = res.session['key']

  File.open(ENV['HOME'] + '/.lily.json', 'w+') do |file|
    file.write(JSON.pretty_generate({"session_key" => sk}))
  end
end

#tokenObject



48
49
50
51
52
53
# File 'lib/lily.rb', line 48

def token
  url = "http://ws.audioscrobbler.com/2.0/?method=auth.getToken&api_key=#{@keys.public}&api_sig=#{@keys.private}&format=json"
  res = JSON.parse(HTTP.get(url))
  # return res.token
  res.token
end

#versionObject



115
116
117
# File 'lib/lily.rb', line 115

def version
  Version::STRING
end