pqdl 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/pqdl +157 -0
- metadata +77 -0
data/bin/pqdl
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "geocaching"
|
4
|
+
require "tempfile"
|
5
|
+
require "yaml"
|
6
|
+
require "zip/zip"
|
7
|
+
|
8
|
+
CONFIG_PATH = File.expand_path("~/.pqdl.yml")
|
9
|
+
|
10
|
+
def usage
|
11
|
+
$stderr.puts "Usage: pqdl list"
|
12
|
+
$stderr.puts " pqdl download [guid]"
|
13
|
+
$stderr.puts " pqdl configure <username> <password>"
|
14
|
+
exit 1
|
15
|
+
end
|
16
|
+
|
17
|
+
def fatal(message)
|
18
|
+
$stderr.puts message
|
19
|
+
exit 1
|
20
|
+
end
|
21
|
+
|
22
|
+
def configure
|
23
|
+
unless ARGV[1] and ARGV[2]
|
24
|
+
fatal "Usage: pqdl configure <username> <password>"
|
25
|
+
end
|
26
|
+
|
27
|
+
File.open(CONFIG_PATH, "w") do |f|
|
28
|
+
f.write({ "username" => ARGV[1], "password" => ARGV[2] }.to_yaml)
|
29
|
+
end
|
30
|
+
|
31
|
+
File.new(CONFIG_PATH).chmod(0700)
|
32
|
+
end
|
33
|
+
|
34
|
+
def read_config
|
35
|
+
unless File.exists?(CONFIG_PATH)
|
36
|
+
fatal "Missing config file #{CONFIG_PATH}"
|
37
|
+
end
|
38
|
+
|
39
|
+
config = YAML.load(File.read(CONFIG_PATH))
|
40
|
+
|
41
|
+
if config["username"] and config["password"]
|
42
|
+
$username = config["username"]
|
43
|
+
$password = config["password"]
|
44
|
+
else
|
45
|
+
fatal "Missing username or password in config file"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def open_session
|
50
|
+
begin
|
51
|
+
$session = Geocaching::Session.open($username, $password)
|
52
|
+
rescue Geocaching::Error
|
53
|
+
fatal "Could not connect to geocaching.com"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def list
|
58
|
+
pocket_queries = begin
|
59
|
+
$session.get_pocket_queries
|
60
|
+
rescue Geocaching::Error
|
61
|
+
fatal "Could not get Pocket Query list from geocaching.com"
|
62
|
+
end
|
63
|
+
|
64
|
+
if pocket_queries and pocket_queries.size > 0
|
65
|
+
pocket_queries.each do |pocket_query|
|
66
|
+
puts " Name: #{pocket_query.name}"
|
67
|
+
puts " GUID: #{pocket_query.guid}"
|
68
|
+
puts " Last generated at: #{pocket_query.generated_at.strftime("%c")}"
|
69
|
+
puts " Results: #{pocket_query.results_count}" if pocket_query.results_count
|
70
|
+
puts
|
71
|
+
end
|
72
|
+
|
73
|
+
puts "Total: #{pocket_queries.size} Pocket Queries"
|
74
|
+
else
|
75
|
+
puts "No Pocket Queries"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def pocket_query_select
|
80
|
+
id = nil
|
81
|
+
|
82
|
+
pocket_queries = begin
|
83
|
+
$session.get_pocket_queries.select do |pocket_query|
|
84
|
+
pocket_query.download_available?
|
85
|
+
end
|
86
|
+
rescue Geocaching::Error
|
87
|
+
fatal "Could not get Pocket Query list from geocaching.com"
|
88
|
+
end
|
89
|
+
|
90
|
+
if pocket_queries.size == 0
|
91
|
+
puts "There are not Pocket Queries available for download"
|
92
|
+
exit
|
93
|
+
end
|
94
|
+
|
95
|
+
puts "Select the Pocket Query you want to download:"
|
96
|
+
|
97
|
+
loop do
|
98
|
+
pocket_queries.each_with_index do |pocket_query, i|
|
99
|
+
puts " (#{i + 1}) #{pocket_query.name}"
|
100
|
+
end
|
101
|
+
|
102
|
+
printf "Pocket Query to download: "
|
103
|
+
id = $stdin.gets.to_i
|
104
|
+
break if id > 0 and id <= pocket_queries.size
|
105
|
+
end
|
106
|
+
|
107
|
+
pocket_queries[id - 1].guid if id
|
108
|
+
end
|
109
|
+
|
110
|
+
def download
|
111
|
+
guid = ARGV[1] || pocket_query_select
|
112
|
+
fatal "Oops" unless guid
|
113
|
+
|
114
|
+
puts "Downloading Pocket Query..."
|
115
|
+
|
116
|
+
pq = begin
|
117
|
+
$session.download_pocket_query_by_guid(guid)
|
118
|
+
rescue Geocaching::Error => e
|
119
|
+
fatal "Could not download Pocket Query from geocaching.com"
|
120
|
+
end
|
121
|
+
|
122
|
+
filename = nil
|
123
|
+
|
124
|
+
tempfile = Tempfile.new(guid)
|
125
|
+
tempfile.write(pq)
|
126
|
+
|
127
|
+
Zip::ZipFile.foreach(tempfile) do |entry|
|
128
|
+
if entry.name =~ /(\d+)\.gpx$/
|
129
|
+
filename = "#{$1}.gpx"
|
130
|
+
File.open(filename, "w") do |f|
|
131
|
+
f.write(entry.get_input_stream.read)
|
132
|
+
end
|
133
|
+
break
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
tempfile.close
|
138
|
+
tempfile.unlink
|
139
|
+
|
140
|
+
fatal "Oops" unless filename
|
141
|
+
puts "Pocket Query written to #{filename}"
|
142
|
+
end
|
143
|
+
|
144
|
+
usage unless ARGV.size > 0 or %w(list download configure).include?(ARGV.first)
|
145
|
+
|
146
|
+
unless ARGV.first == "configure"
|
147
|
+
read_config
|
148
|
+
open_session
|
149
|
+
end
|
150
|
+
|
151
|
+
send(ARGV.first)
|
152
|
+
|
153
|
+
begin
|
154
|
+
$session.close if $session
|
155
|
+
rescue Geocaching::Error
|
156
|
+
#
|
157
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pqdl
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Thomas Cyron
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-04-30 00:00:00 +02:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: geocaching
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 0.8.0
|
25
|
+
type: :runtime
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rubyzip
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: "0"
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id002
|
38
|
+
description: Download Pocket Queries from your command line
|
39
|
+
email: thomas@thcyron.de
|
40
|
+
executables:
|
41
|
+
- pqdl
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- bin/pqdl
|
48
|
+
has_rdoc: true
|
49
|
+
homepage: http://nano.github.com/pqdl
|
50
|
+
licenses: []
|
51
|
+
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.5.0
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: Geocaching.com Pocket Query Downloader
|
76
|
+
test_files: []
|
77
|
+
|