-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbookmarks
executable file
·103 lines (78 loc) · 1.82 KB
/
bookmarks
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
#!/usr/bin/perl
use strict;
use open qw(:utf8);
use utf8;
use WWW::Mechanize;
use Storable;
use WWW::Shorten::Simple;
use Getopt::Long;
use Tie::File;
use Data::Dumper;
{
package Data::Dumper;
no strict "vars";
$Terse = $Indent = $Useqq = $Deparse = $Sortkeys = 1;
$Quotekeys = 0;
}
my $m = WWW::Mechanize->new(
onwarn => undef,
onerror => undef,
);
$m->agent_alias('Linux Mozilla');
my $bm_raw = "$ENV{HOME}/usr/share/doc/links";
my $bm_db = "$ENV{HOME}/usr/share/doc/links_db";
my %bookmarks = ();
my $opt;
$opt->{cached} = 0;
GetOptions(
'rehash|regen' => sub { unlink($bm_db) },
);
my $pattern = shift;
if(-f $bm_db) {
$opt->{cached} = 1;
%bookmarks = %{ retrieve($bm_db) };
}
else {
warn "Generating bookmarks database...\n";
%bookmarks = %{ process_url($bm_raw) };
_store( \%bookmarks );
}
printf("%s titles shown:\n", ($opt->{cached}) ? 'Cached' : 'Fresh');
my(%seen, %dupe);
for my $u(sort{ $bookmarks{$a} cmp $bookmarks{$b} } keys(%bookmarks)) {
if($seen{ $bookmarks{$u} } > 1) {
$dupe{ $bookmarks{$u} }++;
next;
}
$seen{ $bookmarks{$u} }++;
if($pattern) {
if( ($bookmarks{$u} =~ m/$pattern/i) or ($u =~ m/$pattern/) ) {
printf("%60.60s %s\n", $bookmarks{$u}, $u);
}
next;
}
printf("%60.60s %s\n", $bookmarks{$u}, $u);
}
printf("\n'$_' was seen %d times\n", $dupe{$_} + 1) for keys %dupe;
sub process_url {
my $raw = shift;
my %result;
$opt->{cached} = 0;
open(my $fh, '<', $raw) or die($!);
while(my $url = <$fh>) {
chomp($url);
$m->get($url) or next;
my $short_url;
do {
my $ss = WWW::Shorten::Simple->new('Googl');
$short_url = eval { $ss->shorten($url); };
not defined $short_url and next;
};
$result{ $short_url } = $m->title;
}
return \%result;
}
sub _store {
my $ref = shift;
store( $ref, $bm_db );
}