-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRakefile
195 lines (157 loc) · 3.9 KB
/
Rakefile
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
require "bundler/gem_tasks"
require_relative "./lib/get"
require_relative "./lib/station"
require "colorize"
require "pp"
require "hirb"
require "rest-client"
require "postrank-uri"
require "progress_bar"
def log(station, message, color)
puts "[#{station.config.id}] #{message.send(color)}\n"
end
task default: [:run]
task :run do
get = Get.new
Station.stations.each do |station|
if ENV["STATION"] and not station.config.id.include?(ENV["STATION"])
next
end
if station.config.disabled
next # log(station,"disabled", :black)
end
unless data = get.get(station.config)
next log(station,"no data found", :yellow)
end
begin
unless data = station.new(data).perform
next log(station,"no result custom station", :red)
end
rescue
next log(station,"error was raised #{$!.message}"[0...100].gsub("\n", "[nl]") + "...", :red)
end
unless data.is_a?(Hash)
next log(station,"wrong return type #{data.class}", :magenta)
end
if data[:song].blank? and data[:artist].blank?
next log(station,"no song and artist found", :blue)
end
if data[:song].blank?
next log(station,"no song found", :white)
end
if data[:artist].blank?
next log(station,"no song artist", :white)
end
log(station,"OK", :green)
pp data if ENV["EXPAND"]
end
end
class ExtractLink
attr_reader :url
def initialize(url)
@url = PostRank::URI.normalize(url)
end
def process
json["log"]["entries"].each_with_object([]) do |e, res|
url = e["request"]["url"]
res << url if valid_url?(url)
end
end
private
def sniff
"./netsniff.js"
end
def valid_url?(url)
[
"fbcdn",
"fbstatic",
"gstatic",
"google",
"facebook"
].all?{ |r| not url.match(r) }
end
def json
JSON.parse(`phantomjs #{sniff} #{url} 2> /dev/null`)
end
end
task :validate_url do
puts "Downloading links ..."
links = ExtractLink.new(ENV.fetch("url")).process
ResultC = Struct.new(:song, :artist, :worker, :link, :header)
LinkC = Struct.new(:link, :header)
ImageC = Struct.new(:image, :logo?)
RejectedC = Struct.new(:header)
result = []
images = []
used_links = []
rejected = []
puts "Found #{links.count} links"
pb = ProgressBar.new
links.each do |link|
pb.increment!
next unless header = header(link)
if image?(header)
next (images << link)
end
unless valid?(header)
next (rejected << header)
end
next unless data = get(link)
used_links << [link, header]
Station.stations.each do |station|
res = station.new(data).perform rescue nil
if valid_data?(res)
result << ResultC.new(
res[:song][:title],
res[:artist][:name],
station.config.id,
link,
header
)
end
end
end
used_links = used_links.map{ |data| LinkC.new(*data) }
images = images.map{ |link| ImageC.new(link, !! (link =~ /logo/)) }
rejected = rejected.uniq.map{ |header| RejectedC.new(header) }
extend Hirb::Console
puts "Used links"
table used_links, fields: [:link, :header]
puts "Images"
table images, fields: [:image, :logo?]
puts "Rejected headers"
table rejected, fields: [:header]
if result.any?
puts "Result"
table result, fields: [:song, :artist, :worker, :link, :header]
else
puts "No results found"
end
end
def header(url)
RestClient.head(url, {
timeout: 3,
open_timeout: 3
}).headers[:content_type]
rescue RestClient::Exception
end
def image?(header)
header.include?("image")
end
def valid_data?(data)
data and
data[:song][:title].length < 30 and
data[:artist][:name].length < 30
end
def plain?(header)
header.include?("html")
end
def json?(header)
header.include?("json")
end
def valid?(header)
plain?(header) or json?(header)
end
def get(link)
Get.new.get(link)
end