iTunesAmazon 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/i_tunes_amazon.rb +23 -11
- data/test/test_i_tunes_amazon.rb +13 -2
- metadata +1 -1
data/lib/i_tunes_amazon.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
#!/usr/bin/env ruby -w
|
2
2
|
|
3
|
-
|
4
|
-
|
5
3
|
require 'rubygems'
|
6
4
|
require 'nokogiri'
|
7
5
|
require 'amazon/aws'
|
@@ -11,10 +9,10 @@ require './lib/track.rb'
|
|
11
9
|
include Amazon::AWS
|
12
10
|
include Amazon::AWS::Search
|
13
11
|
|
14
|
-
KEY_ID = '
|
12
|
+
KEY_ID = ''
|
15
13
|
|
16
14
|
class ITunesAmazon
|
17
|
-
VERSION = '0.0.
|
15
|
+
VERSION = '0.0.2'
|
18
16
|
|
19
17
|
attr_accessor :tracks, :albums, :total_time
|
20
18
|
|
@@ -25,6 +23,7 @@ class ITunesAmazon
|
|
25
23
|
get_amazon_stats
|
26
24
|
end
|
27
25
|
|
26
|
+
# Creates the tracks and albums objects from an iTunes file
|
28
27
|
def parse(file)
|
29
28
|
xml = Nokogiri::XML.parse(File.read(file))
|
30
29
|
xml.xpath("//dict/dict/dict").each do |song|
|
@@ -40,7 +39,11 @@ class ITunesAmazon
|
|
40
39
|
when 'Play Count' then track.play_count = attr.next.text
|
41
40
|
end
|
42
41
|
end
|
43
|
-
|
42
|
+
|
43
|
+
# don't deal with Podcasts
|
44
|
+
unless track.genre == 'Podcast' then @tracks << track end
|
45
|
+
|
46
|
+
# Set up a hash of albums.
|
44
47
|
key = "#{track.artist},#{track.album}"
|
45
48
|
unless @albums.has_key?(key)
|
46
49
|
@albums[key] = Album.new(track.album, track.artist, track)
|
@@ -50,6 +53,7 @@ class ITunesAmazon
|
|
50
53
|
end
|
51
54
|
end
|
52
55
|
|
56
|
+
# Runs Amazon stats on each album
|
53
57
|
def get_amazon_stats
|
54
58
|
@albums.each_value do |album|
|
55
59
|
album.get_amazon_info
|
@@ -88,7 +92,7 @@ class ITunesAmazon
|
|
88
92
|
end
|
89
93
|
|
90
94
|
def random_song(seed = nil)
|
91
|
-
|
95
|
+
srand seed if seed
|
92
96
|
@tracks[rand(@tracks.size)]
|
93
97
|
end
|
94
98
|
|
@@ -117,18 +121,26 @@ class ITunesAmazon
|
|
117
121
|
fields.sort {|a, b| b[1] <=> a[1]}[0][0]
|
118
122
|
end
|
119
123
|
|
120
|
-
def
|
121
|
-
top_albums = @albums.values.
|
124
|
+
def top_selling_amazon_albums(n)
|
125
|
+
top_albums = @albums.values.sort_by {|album| album.amazon_sales_rank.to_i }
|
122
126
|
top_albums[0..(n-1)]
|
123
127
|
end
|
124
128
|
|
125
|
-
|
129
|
+
def how_much_is_it_worth
|
130
|
+
amount = 0
|
131
|
+
@albums.values.each do |album|
|
132
|
+
#unless (track.time == nil && track.time.is_a?(Numeric)) then
|
133
|
+
amount += album.amazon_price.to_i
|
134
|
+
# end
|
135
|
+
end
|
136
|
+
amount
|
137
|
+
end
|
126
138
|
|
127
139
|
def self.run(args)
|
128
140
|
parser = ITunesAmazon.new(args[0])
|
129
141
|
|
130
|
-
album = parser.
|
131
|
-
p "Top
|
142
|
+
album = parser.top_selling_amazon_albums(1)[0]
|
143
|
+
p "Top selling Amazon album: #{album.album} by #{album.artist} sell rank #{album.amazon_sales_rank}"
|
132
144
|
|
133
145
|
top_track = parser.top_played_songs(1)[0]
|
134
146
|
p "Top song: #{top_track.name} by #{top_track.artist}, played #{top_track.play_count} times"
|
data/test/test_i_tunes_amazon.rb
CHANGED
@@ -1,12 +1,11 @@
|
|
1
1
|
#!/usr/bin/env ruby -w
|
2
2
|
|
3
|
-
|
4
3
|
$: << 'lib'
|
5
4
|
|
6
5
|
require "test/unit"
|
7
6
|
require 'i_tunes_amazon.rb'
|
8
7
|
|
9
|
-
class
|
8
|
+
class ITunesAmazonTester < Test::Unit::TestCase
|
10
9
|
@@parse_this = ITunesAmazon.new("../test.itunes.xml")
|
11
10
|
|
12
11
|
def setup
|
@@ -73,5 +72,17 @@ class ITunesParserTester < Test::Unit::TestCase
|
|
73
72
|
assert_equal expected, actual
|
74
73
|
end
|
75
74
|
|
75
|
+
def test_amazon_top_selling_album
|
76
|
+
@parser.get_amazon_stats
|
77
|
+
actual = @parser.top_selling_amazon_albums(1)[0].album
|
78
|
+
expected = "Live In Cook County Jail"
|
79
|
+
assert_equal expected, actual
|
80
|
+
end
|
76
81
|
|
82
|
+
def test_how_much_is_it_worth
|
83
|
+
@parser.get_amazon_stats
|
84
|
+
actual = @parser.how_much_is_it_worth
|
85
|
+
expected = 3547
|
86
|
+
assert_equal expected, actual
|
87
|
+
end
|
77
88
|
end
|