This repository has been archived by the owner on Mar 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
planes.rb
81 lines (77 loc) · 3.42 KB
/
planes.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
class Planes
def initialize(name, type, nation, epoch, url)
@name = name
@type = type
@nation = nation
@epoch = epoch
@url = url
end
attr_reader :name, :type, :nation, :epoch
end
def encoding_safe_response(url, encoding)
begin
uri_parse = URI.parse(url)
response = Net::HTTP.get(uri_parse)
result = response.dup.force_encoding(encoding)
unless result.valid_encoding?
result = response.encode(encoding, 'Windows-1251' )
end
rescue EncodingError
result.encode!(encoding, invalid: :replace, undef: :replace )
end
end
def best_nation(url, regex)
#Determines the best nation
begin
response_nations = encoding_safe_response(url,'UTF-8')
array_of_nations = response_nations.scan(regex)
return array_of_nations.max_by{|x| x.count.to_i}[0]
rescue
return 'Другие'
end
end
$urls = [
['http://wp.scn.ru/ru/ww1/f/', 'Fighter', 'World War I'],
['http://wp.scn.ru/ru/ww1/b/', 'Bomber', 'World War I'],
['http://wp.scn.ru/ru/ww1/a/', 'Attack', 'World War I'],
['http://wp.scn.ru/ru/ww1/t/', 'Transport', 'World War I'],
['http://wp.scn.ru/ru/ww1/o/', 'Other', 'World War I'],
['http://wp.scn.ru/ru/ww1/h/', 'Sea', 'World War I'],
['http://wp.scn.ru/ru/ww1/s/', 'Special', 'World War I'],
['http://wp.scn.ru/ru/ww1/v/', 'Helicopter', 'World War I'],
['http://wp.scn.ru/ru/ww15/f/', 'Fighter', 'Interwar'],
['http://wp.scn.ru/ru/ww15/b/', 'Bomber', 'Interwar'],
['http://wp.scn.ru/ru/ww15/a/', 'Attack', 'Interwar'],
['http://wp.scn.ru/ru/ww15/t/', 'Transport', 'Interwar'],
['http://wp.scn.ru/ru/ww15/o/', 'Other', 'Interwar'],
['http://wp.scn.ru/ru/ww15/h/', 'Sea', 'Interwar'],
['http://wp.scn.ru/ru/ww15/s/', 'Special', 'Interwar'],
['http://wp.scn.ru/ru/ww15/v/', 'Helicopter', 'Interwar'],
['http://wp.scn.ru/ru/ww2/f/', 'Fighter', 'World War II'],
['http://wp.scn.ru/ru/ww2/b/', 'Bomber', 'World War II'],
['http://wp.scn.ru/ru/ww2/a/', 'Attack', 'World War II'],
['http://wp.scn.ru/ru/ww2/t/', 'Transport', 'World War II'],
['http://wp.scn.ru/ru/ww2/o/', 'Other', 'World War II'],
['http://wp.scn.ru/ru/ww2/h/', 'Sea', 'World War II'],
['http://wp.scn.ru/ru/ww2/s/', 'Special', 'World War II'],
['http://wp.scn.ru/ru/ww2/v/', 'Helicopter', 'World War II'],
['http://wp.scn.ru/ru/ww3/f/', 'Fighter', 'Cold War'],
['http://wp.scn.ru/ru/ww3/b/', 'Bomber', 'Cold War'],
['http://wp.scn.ru/ru/ww3/a/', 'Attack', 'Cold War'],
['http://wp.scn.ru/ru/ww3/t/', 'Transport', 'Cold War'],
['http://wp.scn.ru/ru/ww3/o/', 'Other', 'Cold War'],
['http://wp.scn.ru/ru/ww3/h/', 'Sea', 'Cold War'],
['http://wp.scn.ru/ru/ww3/s/', 'Special', 'Cold War'],
['http://wp.scn.ru/ru/ww3/v/', 'Helicopter', 'Cold War'],
['http://wp.scn.ru/ru/ww4/f/', 'Fighter', 'Modern'],
['http://wp.scn.ru/ru/ww4/b/', 'Bomber', 'Modern'],
['http://wp.scn.ru/ru/ww4/a/', 'Attack', 'Modern'],
['http://wp.scn.ru/ru/ww4/t/', 'Transport', 'Modern'],
['http://wp.scn.ru/ru/ww4/o/', 'Other', 'Modern'],
['http://wp.scn.ru/ru/ww4/h/', 'Sea', 'Modern'],
['http://wp.scn.ru/ru/ww4/s/', 'Special', 'Modern'],
['http://wp.scn.ru/ru/ww4/v/', 'Helicopter', 'Modern'],
['http://wp.scn.ru/ru/ww4/d/', 'Drone', 'Modern']
]
$planes_regex = /<a\shref=(?<url>[^>]*)>(?<name>[^<]*)<\/a>\s?\[\d+\]<br>/
$nations_regex = /<img\sclass=img_bg[^.]*\.gif>\s<a\shref=[^>]*>(?<country>[^<]*)<\/a>\s?\[(?<count>\d+)\]/