lastfm-tail 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +6 -0
  2. data/bin/lastfm-tail +46 -13
  3. metadata +36 -3
data/README.md CHANGED
@@ -19,8 +19,14 @@ or
19
19
  option. Implies -r.
20
20
  -F, --frequency NUM Poll Last.fm every NUM seconds. Defaults
21
21
  to 5. Implies `-f`.
22
+ --no-translate Don't auto-translate foreign artist names.
22
23
  -n, --number NUM List NUM tracks.
23
24
  -r, --reverse List tracks from oldest to newest.
24
25
  -v, --version Show the installed version of lastfm-tail.
25
26
  -h, --help Show this help dialog.
26
27
 
28
+ If you change options from the defaults frequently, you can specify alternate
29
+ default values in a configuration file. Copy `config.ini.example` to
30
+ `$HOME/.config/lastfm-tail/config.ini` and uncomment the lines you wish to
31
+ change.
32
+
data/bin/lastfm-tail CHANGED
@@ -1,19 +1,28 @@
1
1
  #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ # May you recognize your weaknesses and share your strengths.
5
+ # May you share freely, never taking more than you give.
6
+ # May you find love and love everyone you find.
2
7
 
3
8
  require 'date'
9
+ require 'englify'
10
+ require 'inifile'
11
+ require 'nice-sigint'
4
12
  require 'open-uri'
5
13
  require 'optparse'
6
14
  require 'xmlsimple'
7
15
 
8
- # Show prettier output on interrupt (ctrl+c)
9
- trap("INT") do |s|
10
- puts
11
- exit(s)
12
- end
16
+ translationCache = {}
17
+
18
+ config = IniFile.new("#{Dir.home}/.config/lastfm-tail/config.ini")
19
+ config = config['global']
20
+ trackLimit = config['trackLimit'].nil? ? 10 : config['trackLimit'].to_i
21
+ pollFrequency = config['pollFrequency'].nil? ? nil : config['pollFrequency'].to_i
22
+ reverse = config['reverse'].nil? ? false : config['reverse'] != 'false'
23
+ autoTranslate = config['autoTranslate'].nil? ? true : config['autoTranslate'] != 'false'
24
+ username = config['username'].nil? ? 'xiongchiamiov' : config['username']
13
25
 
14
- trackLimit = 10
15
- pollFrequency = nil
16
- reverse = false
17
26
  OptionParser.new do |opts|
18
27
  opts.banner = "Usage: #{$0.split('/').last} [options] [USER]"
19
28
 
@@ -30,6 +39,10 @@ OptionParser.new do |opts|
30
39
  reverse = true
31
40
  end
32
41
 
42
+ opts.on('--no-translate', "Don't auto-translate foreign artist names.") do
43
+ autoTranslate = false
44
+ end
45
+
33
46
  opts.on('-n', '--number NUM', Integer, 'List NUM tracks.') do |num|
34
47
  if num > 10
35
48
  puts 'Last.fm only provides the 10 latest tracks; sorry!'
@@ -44,7 +57,7 @@ OptionParser.new do |opts|
44
57
  end
45
58
 
46
59
  opts.on('-v', '--version', 'Show the installed version of lastfm-tail.') do
47
- puts 'lastfm-tail 1.0.2'
60
+ puts 'lastfm-tail 1.1.0'
48
61
  exit 0
49
62
  end
50
63
 
@@ -54,12 +67,24 @@ OptionParser.new do |opts|
54
67
  end
55
68
  end.parse!
56
69
 
57
- username = ARGV[-1] || 'xiongchiamiov'
70
+ username = ARGV[-1] || username
58
71
  lastTimestamp = Time.at(0)
59
72
 
60
73
  # The logic here dealing with polling is a bit dirty.
61
74
  begin
62
- rss = open("http://ws.audioscrobbler.com/2.0/user/#{username}/recenttracks.rss").read()
75
+ begin
76
+ rss = open("http://ws.audioscrobbler.com/2.0/user/#{username}/recenttracks.rss").read()
77
+ rescue SocketError, OpenURI::HTTPError, EOFError, Net::HTTPServiceUnavailable
78
+ $stderr.write "Network unreachable, trying again in "
79
+ 5.downto 1 do |i|
80
+ $stderr.print "#{i} "
81
+ sleep 1
82
+ end
83
+ $stderr.write "\n"
84
+ networkError = true
85
+ next
86
+ end
87
+ networkError = false
63
88
  xml = XmlSimple.xml_in rss
64
89
  items = xml['channel'][0]['item']
65
90
  items.reverse! if reverse
@@ -74,8 +99,16 @@ begin
74
99
  next
75
100
  end
76
101
 
77
- puts item['title']
102
+ artist, title = item['title'][0].split(' – ')
103
+ if autoTranslate && !artist.ascii_only?
104
+ if translationCache[artist].nil?
105
+ translationCache[artist] = englify(artist)
106
+ end
107
+ artist = translationCache[artist]
108
+ end
109
+ puts "#{artist} - #{title}"
110
+
78
111
  lastTimestamp = timestamp
79
112
  end
80
- end while !pollFrequency.nil? && sleep(pollFrequency)
113
+ end while networkError || (!pollFrequency.nil? && sleep(pollFrequency))
81
114
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: lastfm-tail
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.2
5
+ version: 1.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - xiongchiamiov
@@ -10,11 +10,11 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-01-27 00:00:00 -08:00
13
+ date: 2013-06-21 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
- name: xml-simple
17
+ name: englify
18
18
  prerelease: false
19
19
  requirement: &id001 !ruby/object:Gem::Requirement
20
20
  none: false
@@ -24,6 +24,39 @@ dependencies:
24
24
  version: "0"
25
25
  type: :runtime
26
26
  version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: inifile
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: 1.1.0
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: nice-sigint
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: xml-simple
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ type: :runtime
59
+ version_requirements: *id004
27
60
  description: A small script, inspired by the Unix tail utility, for viewing recently-scrobbled tracks on Last.fm.
28
61
  email:
29
62
  - xiong.chiamiov@gmail.com