How can I get the numbers from the following website (using nokogiri) - ruby

My code was working but now I am able to get any info from the site apart from the numbers that i need.
The code would be
require 'open-uri'
require 'nokogiri'
require 'curb'
require 'json'
require 'pp'
require 'openssl'
ejnumber4 = Nokogiri::HTML(open('https://www.sazka.cz/loterie/eurojackpot/sazky-a-vysledky'))
ejnumber4.css('.number-row').css('.numbers').text
What I am missing?
Thank you.

The website gets the data from 2 consecutives API calls. The first one is to get the past draws list with the date and draw ID:
GET https://www.sazka.cz/api/draw-info/past-draws/eurojackpot
The second one is to get the draw result for a specific draw ID:
GET https://www.sazka.cz/api/draw-info/draws/universal/eurojackpot/[DRAW_ID]
You can try the following bash script using curl and jq to get the last draw result:
#!/bin/bash
id=$(curl -s "https://www.sazka.cz/api/draw-info/past-draws/eurojackpot" | jq -r '.[0].id')
curl -s "https://www.sazka.cz/api/draw-info/draws/universal/eurojackpot/$id" | jq '.'
Using ruby with net/http:
require 'net/http'
require 'json'
res = Net::HTTP.get(URI("https://www.sazka.cz/api/draw-info/past-draws/eurojackpot"))
id = JSON.parse(res)[0]["id"]
res = Net::HTTP.get(URI("https://www.sazka.cz/api/draw-info/draws/universal/eurojackpot/#{id}"))
p JSON.parse(res)
Using ruby with open-uri:
require 'open-uri'
require 'json'
res = open("https://www.sazka.cz/api/draw-info/past-draws/eurojackpot").read
id = JSON.parse(res)[0]["id"]
res = open("https://www.sazka.cz/api/draw-info/draws/universal/eurojackpot/#{id}").read
p JSON.parse(res)
Output example:
{ "drawId"=>2021255, "drawDate"=>"2021-06-25T19:00:00", "week"=>25, "year"=>2021, "draws"=>[[30, 4, 22, 47, 17], [1, 8]], "drawsExtra"=>nil, "addonDraw"=>[4, 0, 6, 9, 4, 3] .....
......

Related

scanning a webpage for urls with ruby and regex

I'm trying to create an array of all links found at the below url. Using page.scan(URI.regexp) or URI.extract(page) returns more than just urls.
How do I get just the urls?
require 'net/http'
require 'uri'
uri = URI("https://gist.github.com/JsWatt/59f4b8ce6bbf0c7e4dc7")
page = Net::HTTP.get(uri)
p page.scan(URI.regexp)
p URI.extract(page)
If you are just trying to extract links (<a href="..."> elements) from the text file then it seems better to parse it as real HTML with Nokogiri, and then extract the links this way:
require 'nokogiri'
require 'open-uri'
# Parse the raw HTML text
doc = Nokogiri.parse(open('https://gist.githubusercontent.com/JsWatt/59f4b8ce6bbf0c7e4dc7/raw/c340b3fbcab7923e52e5b50165432b6e5f2e3cf4/for_scraper.txt'))
# Extract all a-elements (HTML links)
all_links = doc.css('a')
# Sort + weed out duplicates and empty links
links = all_links.map { |link| link.attribute('href').to_s }.uniq.
sort.delete_if { |h| h.empty? }
# Print out some of them
puts links.grep(/store/)
http://store.steampowered.com/app/214590/
http://store.steampowered.com/app/218090/
http://store.steampowered.com/app/220780/
http://store.steampowered.com/app/226720/
...

How to parse a URL using Ruby

Hi
how i print
http://site.tf/home/
from
http://site.tf/home/index.php?id=12
using ruby parse url
Do like this
require 'uri'
uri = URI.parse('http://site.tf/home/index.php?id=12')
"#{uri.scheme}://#{uri.host}/#{uri.path.split('/')[1]}"
#=> "http://site.tf/home/"
Didn't tested though.I guess it should work fine
Update
If you want just site.tf,just do like this
require 'uri'
uri = URI.parse('http://site.tf/home/index.php?id=12')
uri.host.split('/').first
#=> "site.tf"

How to handle a json file return by the server with ruby?

I have a json file return by a web radio
require 'open-uri'
rquiire 'json'
songlist=open('http://douban.fm/j/mine/playlist?type=n&channel=0')
##this will return a json file:
##{"r":0,"song" [{"album":"\/subject\/25863639\/","picture":"http:\/\/img5.douban.com\/mpic\/s27256956.jpg","ssid":"7656","artist":"Carousel Kings","url":"http:\/\/mr3.douban.com\/201404122019\/660a1b4494a255e0333dfdc9ffadcf08\/view\/song\/small\/p2055547.mp3","company":"Not On Label","title":"Silence","rating_avg":3.73866,"length":194,"subtype":"","public_time":"2014","sid":"2055547","aid":"25863639","sha256":"ebf027adfaf9882118456941a774eeb509c29c4c278f55f587ba2faaa858a49d","kbps":"64","albumtitle":"Unity","like":false}]
I want to get the information like this song[0]['url'], song[0]['title'],song[0]['album']and using smplayer in terminal to play the song by pointed by url.
How can i do that with ruby?
Thanks.
I would use JSON.parse as below
require 'open-uri'
require 'json'
songlist = open('http://douban.fm/j/mine/playlist?type=n&channel=0').read
parsed_songlist = JSON.parse(songlist)
parsed_songlist["song"][0]["url"] #=> "http:\/\/mr3.douban.com\/201404122019\/660a1b4494a255e0333dfdc9ffadcf08\/view\/song\/small\/p2055547.mp3"
parsed_songlist["song"][0]["title"] #=> "Silence"

Testing filepicker.io security using Ruby

I'm trying to build a test that will allow me to exercise FilePicker.io security. The code is run as:
ruby test.rb [file handle]
and the result is the query string that I can append to a FilePicker URL. I'm pretty sure my policy is getting read properly, but my signature isn't. Can someone tell me what I'm doing wrong? Here's the code:
require 'rubygems'
require 'base64'
require 'cgi'
require 'openssl'
require 'json'
handle = ARGV[0]
expiry = Time::now.to_i + 3600
policy = {:handle=>handle, :expiry=>expiry, :call=>["pick","read", "stat"]}.to_json
puts policy
puts "\n"
secret = 'SECRET'
encoded_policy = CGI.escape(Base64.encode64(policy))
signature = OpenSSL::HMAC.hexdigest('sha256', secret, encoded_policy)
puts "?signature=#{signature}&policy=#{encoded_policy}"
The trick is to use Base64.urlsafe_encode64 instead of CGI.escape:
require 'rubygems'
require 'base64'
require 'cgi'
require 'openssl'
require 'json'
handle = ARGV[0]
expiry = Time::now.to_i + 3600
policy = {:handle=>handle, :expiry=>expiry}.to_json
puts policy
puts "\n"
secret = 'SECRET'
encoded_policy = Base64.urlsafe_encode64(policy)
signature = OpenSSL::HMAC.hexdigest('sha256', secret, encoded_policy)
puts "?signature=#{signature}&policy=#{encoded_policy}"
When tested with the sample values for expiry, handle, and secret in the Filepicker.io docs it returns same values as the python example.
I resolved this in my Ruby 1.8 environment by removing the CGI.escape and gsubbing out the newline:
Base64.encode64(policy).gsub("\n","")
elevenarms's answer is the best for Ruby 1.9 users, but you have to do something a bit kludgy like the above for Ruby 1.8. I'll accept his answer nonetheless, since most of us are or shortly will be in 1.9 these days.

Nesting error when trying to parse JSONfile

I'm trying to parse the JSON file of a Reddit thread with all it's comments. But when I try to parse the JSON I get a "in `parse': nesting of 20 is too deep " error.
Below is the code i use:
#require 'net/http'
#require 'rubygems'
#require 'json'
#response = Net::HTTP.get(URI.parse("http://www.reddit.com/r/AskReddit/comments/sjm1z/what_is_your_most_useless_talent/.json"))
result = JSON.parse(#response)
Is there anyway I can get around this?
It is not essential for me to have all the smaller subthreads parsed. Is there a way to set the nesting depth limit?
Try setting the max_nesting value:
result = JSON.parse(#response, :max_nesting => 100)

Resources