Created
August 18, 2014 21:21
-
-
Save nikita-barsukov/e6b039f2d5e251a31196 to your computer and use it in GitHub Desktop.
Pars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/https' | |
require 'nokogiri' | |
def parse_wout(w) | |
uri = URI.parse "https://www.endomondo.com/workouts/#{w}" | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
req = Net::HTTP::Get.new uri.path | |
r = http.request(req) | |
str = "" | |
if r.code == '200' | |
raw_body = r.body | |
doc = Nokogiri::XML(raw_body) | |
j = " " | |
name = doc.css('.profile-badge h1').text | |
str << "'#{w}'" | |
str << "\t" | |
str << "'#{doc.css('.sport-name').text}'" | |
str << "\t" | |
str << "'#{doc.css('.date-time').text}'" | |
str << "\t" | |
str << "'#{name}'" | |
str << "\t" | |
doc.css("script").each do |s| | |
if s.text.include? '"data":' | |
begin | |
j = s.text.gsub("\n", "") | |
j = j.scan(/"data":(.*?\])/)[0][0] | |
rescue | |
next | |
end | |
end | |
end | |
str << "'#{j}'" | |
str << "\t" | |
nodes = %w(distance duration avg-speed max-speed calories) | |
nodes.each do |node| | |
t = doc.css(".side-tabs li.#{node} .value").text | |
str << "'#{t}'" | |
str << "\t" | |
end | |
end | |
str.chomp("\t") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment