forked from daveverwer/iOSDevDirectory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
63 lines (52 loc) · 1.8 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
require 'json'
require 'json-schema'
require 'faraday'
task default: 'validate:json'
namespace :validate do
desc 'Validate the JSON schema and the blogs JSON content'
task :json do
JSON::Validator.validate!('schema_blogs.json', 'blogs.json')
end
desc "Look for redirects in the site or feed URLs"
task :redirects do
file = File.read('blogs.json')
data = JSON.parse(file)
data.each do |language|
language['categories'].each do |category|
category['sites'].each do |site|
%w[site_url feed_url].each do |field|
response = Faraday.head(site[field])
if response.status.between?(300, 399)
new_uri = URI(response.headers['location'])
# There are lots of incorrect redirects for YouTube channels.
next if new_uri.host == 'consent.youtube.com'
unless new_uri.is_a?(URI::HTTP) || new_uri.is_a?(URI::HTTPS)
path = new_uri.path
new_uri = URI(site[field])
new_uri.path = path
end
puts "HTTP #{response.status} for #{site['title']}", site[field], new_uri, "\n"
site[field] = new_uri.to_s
end
rescue Faraday::Error
# Ignore connection errors and 404s
end
end
end
end
File.write('new_blogs.json', JSON.pretty_generate(data))
end
end
namespace :sort do
desc 'Re-sort the sites in all categories'
task :blogs do
data = JSON.parse(File.read('blogs.json'))
# Sort sites in each category by title
data.each do |language|
language['categories'].each do |category|
category['sites'].sort_by! { |site| site['title'].delete_prefix('The ').downcase }
end
end
File.write('new_blogs.json', JSON.pretty_generate(data))
end
end