-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.rb
executable file
·112 lines (76 loc) · 2.44 KB
/
get.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env ruby
# um Ergebnisse aus Endomondo zu scrapen
# 24.12.2013
#
require 'mechanize'
require 'nokogiri'
require 'rubygems'
require 'logger'
require 'open-uri'
pages = 30
def clean(b)
c = b.gsub(/\t\t/,'').gsub(/ \n\t/,'').gsub(/\n /,'').gsub(/ /,'').gsub(/ /,',').gsub(/ $/,'').gsub(/^D.*/,'').gsub(/^\n/,'')
return c
end
# build cache
expiration = 60 * 60
# file to store cache in
cache = "cache.txt"
cache_age = Time.new - File.new('cache.txt').mtime
a = Mechanize.new do |agent|
agent.user_agent_alias = 'Mac Safari'
agent.log = Logger.new(STDOUT)
agent.log.level = Logger::ERROR
end
# if the cache age is greater than our expiration time
if cache_age > expiration
# our cache has expired
puts "cache has expired. fetching new headline"
a.get ('https://www.endomondo.com/access') do |login_page|
my_page = login_page.form_with(:action => '?wicket:interface=:0:pageContainer:lowerSection:lowerMain:lowerMainContent:signInPanel:signInFormPanel:signInForm::IFormSubmitListener::') do |form|
form.email = 'XXXXXXXXXXXXXXXXXXX'
form.password = 'XXXXXXXXXXXXXXXXXXX'
end.submit
# jetzt suchen: workouts
# debug puts '->,<-'
workout_page = my_page.link_with(:text => 'WORKOUTS').click
# jetzt einzelne workouts
ww = workout_page.link_with(:text => 'Historie').click
# jetzt Ergebnisse finden
page = Nokogiri::HTML(ww.body)
# debug pp page
rows = page.xpath("//table/tbody/tr[position() > 0]")
header = rows.shift
a = clean(rows.text)
# naechste Seite
# Schleife bauen (i=0 to pages)
arr = Array.new(pages)
arr[0] = a
puts arr[0]
# temp variable
interim = ww
# debug pp ww
(2..pages).each do |i|
interim = interim.link_with(:text => "#{i}").click
page = Nokogiri::HTML(interim.body)
rows = page.xpath("//table/tbody/tr[position() > 0]")
header = rows.shift
result = clean(rows.text)
arr[i-1] = result
puts arr[i-1]
end
# write file
File.open(cache, "w") do |file|
(0..pages).each do |i|
file.puts arr[i]
end
end
puts "cache updated"
end
else
# we should use our cached copy
# read cache into a string using the read method
data = IO.read("cache.txt")
puts data
puts "used cached copy"
end