-
Notifications
You must be signed in to change notification settings - Fork 1
/
external-url-filter.rb
executable file
·58 lines (53 loc) · 1.22 KB
/
external-url-filter.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
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
# $Id$
require "uri"
$KCODE = "utf-8"
def parse_url( url )
uri = nil
begin
uri = URI.parse( url )
rescue URI::InvalidURIError => e
if url.sub!( /[\s\)) ].*\Z/, "" )
uri = URI.parse( url )
else
raise e
end
end
uri
end
def fullpagename( title, ns = 0 )
#ns = ns.to_i
case ns
when 0, "0"
title
else
"{{ns:#{ ns }}}:#{ title }"
end
end
if $0 == __FILE__
count = {}
ARGF.each do |line|
page_id, url, title, ns, = line.chomp.split( /\t/ )
uri = nil
begin
uri = parse_url( url )
rescue URI::InvalidURIError => e
warn "#{ e }\t#{ page_id }"
rescue URI::InvalidComponentError => e
warn "#{ e }\t#{ page_id }"
end
next if uri.nil?
next if uri.scheme == "mailto"
case uri.host
when /twitter\.com\Z/
next
end
next if title =~ /\A(jdarchive\/seeds|Yegusa\/jdarchive)/
count[ url ] ||= []
count[ url ] << fullpagename( title, ns )
end
count.keys.sort_by{|e| [ -1 * count[e].size, e ] }.each do |url|
puts [ url, count[url].map{|e| "[[#{ e }]]" }.sort.join(" ") ].join("\t")
end
end