This repository has been archived by the owner on Jan 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathbookmark-utils
executable file
·233 lines (178 loc) · 4.26 KB
/
bookmark-utils
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/perl
=head1 NAME
bookmark-utils - A utility script for working with bookmarks.
=cut
=head1 SYNOPSIS
bookmark-utils mode [args]
Modes are:
404 - Poll each bookmark and report on states
dump - Show the parsed bookmarks, titles, and tags.
domains - Show the most popular domains in the bookmarks
dupes - Show duplicate URLs
=cut
=head1 ABOUT
This script contains a couple of utility functions for checking on
the bookmarks:
=over 8
=item c<404>
Use the L<LWP::UserAgent> module to fetch each bookmark, and report on
success or failure.
=item c<domains>
Report on the most bookmarked domain-names. This is useful to find
potential new tags (for example "wikipedia" might become an obvious
tag to add, if you have many links pointing there).
=item c<dump [pattern]>
Show all bookmarks, if a pattern is specified it will be used to
filter the output to include only matching entries.
=item c<dupes>
Report on any URL which is bookmarked more than once, which might
happen if you merge bookmarks from multiple sources.
=back
=cut
=head1 LICENSE
This module is free software; you can redistribute it and/or modify it
under the terms of either:
a) the GNU General Public License as published by the Free Software
Foundation; either version 2, or (at your option) any later version,
or
b) the Perl "Artistic License".
=cut
=head1 AUTHOR
Steve Kemp <[email protected]>
=cut
use strict;
use warnings;
use Data::Dumper;
use Getopt::Long;
use LWP::UserAgent;
#
# Get the mode
#
my $mode = shift || undef;
if ( ( !defined($mode) ) ||
( $mode !~ /^(404|dump|dupes|domains)$/i ) )
{
print <<EOF;
Usage: $0 [404|dupes|domains]
EOF
exit(1);
}
#
# Parse the bookmarks
#
my @bookmarks = parse_bookmarks();
if ( $mode =~ /dump/i )
{
my $arg = shift;
#
# Show each bookmark
#
foreach my $ent (@bookmarks)
{
if ($arg)
{
if ( ( $ent->{ 'title' } =~ /$arg/i ) ||
( $ent->{ 'link' } =~ /$arg/i ) ||
( $ent->{ 'tags' } && ( $ent->{ 'tags' } =~ /$arg/i ) ) )
{
print Dumper( \$ent );
}
}
else
{
print Dumper( \$ent );
}
}
exit(0);
}
if ( $mode =~ /dupes/i )
{
my %seen;
foreach my $entry (@bookmarks)
{
$seen{ $entry->{ 'link' } } += 1;
}
foreach my $link ( keys %seen )
{
print $link . "\n" if ( $seen{ $link } > 1 );
}
exit 0;
}
if ( $mode =~ /domains/i )
{
my %domains;
foreach my $entry (@bookmarks)
{
my $link = $entry->{ 'link' };
if ( $link =~ /:\/\/([^\/]+)\// )
{
$domains{ $1 } += 1;
}
}
foreach
my $dom ( sort {$domains{ $a } <=> $domains{ $b }} ( keys %domains ) )
{
print( sprintf( "%4d - %s\n", $domains{ $dom }, $dom ) );
}
exit 0;
exit 0;
}
if ( $mode =~ /404/ )
{
my $ua = LWP::UserAgent->new;
$ua->agent(
"Mozilla/5.0 (Windows NT 6.3; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0"
);
$ua->timeout(10);
$ua->env_proxy;
foreach my $entry (@bookmarks)
{
my $link = $entry->{ 'link' };
next unless ( $link =~ /^https?:/ );
my $response = $ua->get($link);
if ( $response->is_success )
{
print "OK $link\n";
}
else
{
print "ERR $link - " . $response->status_line() . "\n";
}
}
exit(0);
}
print "Unknown mode\n";
exit 1;
=begin doc
Parse the bookmarks into an array of hashes, containing the links,
titles, and tags.
=end doc
=cut
sub parse_bookmarks
{
my @bookmarks;
my $file = "bookmarks.data";
open( my $handle, "<", $file ) or
die "Failed to open $!";
while ( my $line = <$handle> )
{
chomp($line);
if ( $line =~ /<li(.*)><a href="([^"]+)">([^<]+)</ )
{
my $tags = $1;
my $link = $2;
my $title = $3;
if ( $tags =~ /="([^"]+)"/ )
{
$tags = $1;
}
push( @bookmarks,
{ tags => $tags,
link => $link,
title => $title
} );
}
}
close($handle);
return (@bookmarks);
}