-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscraper.rb
135 lines (104 loc) · 3.19 KB
/
scraper.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'yaml'
# http://www.uitzendinggemist.nl/programmas?order=latest_broadcast_date_asc&page=80
$urls = Array.new
$programUrls = Array.new
$baseUrl = 'http://www.uitzendinggemist.nl'
def has_sub(path)
url = "#{$baseUrl}#{path}"
page = Nokogiri::HTML(open(url))
data = Hash[page.xpath("//span/@*[starts-with(name(), 'data-')]").map{|e| [e.name,e.value]}]
if data.nil?
return false
end
return true
end
def find_episodes(url)
puts "Finding episodes in #{url}"
begin
page = Nokogiri::HTML(open(url))
links = page.css('#episodes li a').map { |link| link['href'] }
if !has_sub(links.first)
return false
end
links.each do |link |
$urls.push(link)
end
nextPage = page.css('.pagination a.next_page')
if nextPage.any?
path = nextPage.map { |link| link['href'] }[0].to_s
last_char = path[path.length-2, path.length-1]
url = "#{$baseUrl}#{path}"
if path.length < 2
return
end
# Remove this part of you want to check all episodes
if last_char.to_i < 30
find_episodes(url)
end
end
rescue
puts "Not found episode #{url}"
end
return true
end
find_episodes('http://www.uitzendinggemist.nl/programmas/2237-wie-is-de-mol')
#find_episodes('http://www.uitzendinggemist.nl/programmas/962-pauw-witteman')
#find_episodes('http://www.uitzendinggemist.nl/programmas/989-de-wereld-draait-door')
#find_episodes('http://www.uitzendinggemist.nl/programmas/1225-pownews')
#find_episodes('http://www.uitzendinggemist.nl/programmas/292-nos-jeugdjournaal-avond')
$urls.each do |path |
next if path == "#"
url = "#{$baseUrl + path}"
puts "Finding data in #{url}"
page = Nokogiri::HTML(open(url))
data = Hash[page.xpath("//span/@*[starts-with(name(), 'data-')]").map{|e| [e.name,e.value]}]
if !data.nil?
id = data['data-player-id']
date = page.css('table.information tr')[1].css('td')[0].text
filename = "subtitles/#{date.to_s}-#{id}.vtt"
open(filename, 'wb') do |file|
file << subtitle = open("http://e.omroep.nl/tt888/#{id}").read
end
end
end
# Simple loop to scrape all programs
=begin
i = 0
while i < 119 do
url = "http://www.uitzendinggemist.nl/programmas?order=latest_broadcast_date_asc&page=#{i}"
puts "Scraping #{url}"
begin
#table.information tr (2de) td text
page = Nokogiri::HTML(open(url))
links = page.css('ol.series.list > li > h2 > a').map { |link| link['href'] }
links.each do |link |
$programUrls.push(link)
end
rescue
puts "Not found #{url}"
end
i += 1
end
$programUrls.each do |path |
url = "#{$baseUrl}#{path}"
find_episodes(url)
end
$urls.each do |path |
next if path == "#"
url = "#{$baseUrl + path}"
puts "Finding data in #{url}"
page = Nokogiri::HTML(open(url))
data = Hash[page.xpath("//span/@*[starts-with(name(), 'data-')]").map{|e| [e.name,e.value]}]
if !data.nil?
id = data['data-player-id']
date = page.css('table.information tr')[1].css('td')[0].text
filename = "subtitles/#{date.to_s}-#{id}.vtt"
open(filename, 'wb') do |file|
file << subtitle = open("http://e.omroep.nl/tt888/#{id}").read
end
end
end
=end