-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththingget_Linux.pl
executable file
·83 lines (69 loc) · 1.78 KB
/
thingget_Linux.pl
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
#!/usr/bin/perl
package MyParser;
use base qw(HTML::Parser);
use LWP::Simple;
my ($thing) = $ARGV[$0] =~ m/([0123456789]+)$/si;
my $url = 'http://www.thingiverse.com/thing:' . $thing;
my $page = get $url;
my $oddcount = 0;
my $isdl = 0;
my @downloads;
my @dlnames;
my ($title) = $page =~ m/<title>(.+)<\/title>/si;
my ($name) = $title =~ m/(.+) by/si;
my ($author) = $title =~ m/by (.+) - Thingiverse/si;
print $name."\n";
print $author;
sub start {
my ($self, $tagname, $attr, $attrseq, $origtext) = @_;
if ($tagname eq 'a') {
if($attr->{ href } =~ 'download') {
if($oddcount == 0) {
push(@downloads, $attr->{ href });
$oddcount = 1;
} else {
$oddcount = 0;
}
}
}
elsif ($tagname eq 'h3') {
if($attr->{class} =~ 'file_info') {
$isdl = 1;
}
}
elsif ($tagname eq 'h2') {
$ish = 1;
}
elsif ($tagname eq 'p' && $ish) {
$isp = 1;
}
}
sub text {
my ($self, $text) = @_;
if ($isdl) {
push(@dlnames, $text);
$isdl = 0;
}
}
package main;
my $parser = MyParser->new;
$parser->parse($page);
my $numdl = @downloads;
system("mkdir -p \"$name\"");
print "\n"."$numdl"." downloads found, downloading..."."\n";
open(OUTDATA, ">.\/$name\/$title.desktop");
print OUTDATA <<"ENDFILE";
[Desktop Entry]
Encoding=UTF-8
Name=$title
Type=Link
URL=http:\/\/www.thingiverse.com\/thing:$thing
Icon=gnome-fs-bookmark
ENDFILE
close(OUTDATA);
for ($i = 0; $i < @downloads; $i++) {
$dlnames[$i] =~ s/^\s+//; #remove leading spaces
$dlnames[$i] =~ s/\s+$//; #remove trailing spaces
print "$dlnames[$i]\n";
system("wget -q http://www.thingiverse.com/"."$downloads[$i]"." -O "."\"$name\"\/\"$dlnames[$i]\"");
}