-
Notifications
You must be signed in to change notification settings - Fork 0
/
netflix.rb
76 lines (55 loc) · 2.17 KB
/
netflix.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
# wthsiwon (WHAT THE HELL SHOULD I WATCH ON NETFLIX) by danrfq
require "open-uri"
require "nokogiri"
def restart()
puts "There's an unexpected error while parsing the information. Restarting the program..." # can't be handled due to inconsistent data structure, sorry
exec("ruby #{__FILE__}")
end
NUMS = (1..170).to_a
NUMS.delete(166)
BASE_URL = "http://whatthehellshouldiwatchonnetflix.com/page/{num}/?random=2" # yes it's not an HTTPS URL
NUM = NUMS.sample.to_s
result = URI.open(BASE_URL.gsub! "{num}", NUM)
data = Nokogiri::HTML(result)
title = data.css("main h1").first.content
clean_title = title.split(" (")[0]
rating = data.css("strong").first.content[1..-1].delete_suffix!("]")
trailer = data.css("main iframe").first.attributes["src"].value.split("?")[0].gsub! "embed/", "watch/?v="
info = data.css("main p")[2..-1][0..-6]
netflix = data.css("main a").map {_1.attributes['href'].value }.find { _1.include?('trackId')}
if info.length == 1
synopsis = info.first
if !synopsis
restart()
end
synopsis = synopsis.content.split("[")[0].gsub "\n", ""
review = nil
else
synopsis = info.first
if !synopsis
restart()
end
synopsis = synopsis.content.split("[")[0].gsub "\n", ""
review = info[1].content
end
if !info.last
restart()
end
if info.last.content.start_with?("Or")
rotten_tomatoes = info[-2].content
rotten_tomatoes_link = info[-2].css("a").first.attributes["href"].value
elsif info.last.content.include?("Rotten Tomatoes")
rotten_tomatoes = info.last.content
rotten_tomatoes_link = info.last.css("a").first.attributes["href"].value
else
rotten_tomatoes = nil
rotten_tomatoes_link = nil
end
puts "wthsiwon (WHAT THE HELL SHOULD I WATCH ON NETFLIX?)\n\n"
puts "Title: #{title}"
rating ? puts("Rating: #{rating}\n") : puts("Rating: -")
rotten_tomatoes ? puts("#{rotten_tomatoes} (#{rotten_tomatoes_link})\n") : puts()
puts "Synopsis: #{synopsis}\n\n"
review ? puts("Review: #{review}\n\n") : puts("Review: -\n\n")
puts "Watch #{clean_title} trailer on YouTube: #{trailer}\n\n"
puts "Watch #{clean_title} in Netflix: #{netflix}"