File Coverage

blib/lib/Rtmgr/Gen/Db.pm
Criterion Covered Total %
statement 20 168 11.9
branch 0 80 0.0
condition n/a
subroutine 7 12 58.3
pod 0 5 0.0
total 27 265 10.1


line stmt bran cond sub pod time code
1             package Rtmgr::Gen::Db;
2              
3 1     1   55950 use 5.006;
  1         4  
4 1     1   4 use strict;
  1         10  
  1         16  
5 1     1   4 use warnings;
  1         1  
  1         36  
6 1     1   451 use XML::RPC;
  1         9914  
  1         27  
7 1     1   431 use Data::Dump qw(dump);
  1         6423  
  1         52  
8 1     1   1338 use DBI;
  1         14957  
  1         54  
9              
10 1     1   8 use Exporter 'import';
  1         1  
  1         1511  
11             our @EXPORT_OK = qw(get_hash create_db_table get_name get_tracker calc_scene);
12            
13             =head1 NAME
14              
15             Rtmgr::Gen::Db - Connect to rTorrent/ruTorrent installation and get a list of torrents, storing them to a database.!
16              
17             =head1 VERSION
18              
19             Version 0.03
20              
21             =cut
22              
23             our $VERSION = '0.03';
24              
25              
26             =head1 SYNOPSIS
27              
28             Connects to a rTorrent/ruTorrent installation.
29              
30             This module connects to an installation of rTorrent/ruTorrent and builds a local SQLite database with the content of the seedbox.
31              
32             =head1 SUBROUTINES/METHODS
33              
34             use Rtmgr::Gen qw(get_hash create_db_table get_name get_tracker);
35              
36             my $create_db = create_db_table('database');
37             print $create_db;
38              
39             my $get_hash = get_hash('user','password','host','443','RPC2','database');
40             print $get_hash;
41              
42             my $get_name = get_name('user','password','host','443','RPC2','database');
43             print $get_name;
44              
45             my $get_tracker = get_tracker('user','password','host','443','RPC2','database');
46             print $get_tracker;
47              
48             =head2 get
49              
50             =cut
51             sub create_db_table {
52 0     0 0   my ($s_file) = @_;
53              
54             # Open SQLite database.
55 0           my $driver = "SQLite";
56 0           my $database = "$s_file.db";
57 0           my $dsn = "DBI:$driver:dbname=$database";
58 0           my $userid = ""; # Not implemented no need for database security on local filesystem at this time.
59 0           my $password = ""; # Not implemented.
60 0 0         my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 }) or die $DBI::errstr;
61              
62 0           print "Opened database successfully\n";
63              
64             # Create the database tables.
65 0           my $stmt = qq(CREATE TABLE SEEDBOX
66             (ID INT PRIMARY KEY NOT NULL,
67             HASH TEXT NOT NULL,
68             SCENE TEXT NOT NULL,
69             TRACKER TEXT NOT NULL,
70             NAME TEXT NOT NULL););
71             # Error checking.
72 0           my $rv = $dbh->do($stmt);
73 0 0         if($rv < 0) {
74 0           print $DBI::errstr;
75             } else {
76 0           print "Table created successfully\n";
77             }
78 0           $dbh->disconnect();
79             }
80              
81              
82             sub get_hash {
83 0     0 0   my ($s_user, $s_pw, $s_url, $s_port, $s_endp, $s_file) = @_;
84             ## Validate input from ARGV
85 0 0         if (not defined $s_user) { die "USEAGE: Missing server user.\n"; }
  0            
86 0 0         if (not defined $s_pw) { die "USEAGE: Missing server password.\n"; }
  0            
87 0 0         if (not defined $s_url) { die "USEAGE: Missing server url.\n"; }
  0            
88 0 0         if (not defined $s_port) { die "USEAGE: Missing server port.\n"; }
  0            
89 0 0         if (not defined $s_endp) { die "USEAGE: Missing server endpoint.\n"; }
  0            
90 0 0         if (not defined $s_file) { die "USEAGE: Missing server db-filename.\n"; }
  0            
91             # Run Example: perl gen-db.pl user pass host port endpoint
92 0           my $xmlrpc = XML::RPC->new("https://$s_user\:$s_pw\@$s_url\:$s_port\/$s_endp");
93 0           my $dl_list = $xmlrpc->call( 'download_list' );
94             # Open SQLite database.
95 0           my $driver = "SQLite";
96 0           my $database = "$s_file.db";
97 0           my $dsn = "DBI:$driver:dbname=$database";
98 0           my $userid = ""; # Not implemented no need for database security on local filesystem at this time.
99 0           my $password = ""; # Not implemented.
100 0 0         my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 }) or die $DBI::errstr;
101              
102 0           print "Opened database successfully\n";
103             # Insert into database each hash returned from $dl_list
104 0           my $n=0;
105 0           foreach my $i (@{ $dl_list}){
  0            
106             #my $name = $xmlrpc->call( 'd.get_name',$i );
107 0           my $stmt = qq(INSERT INTO SEEDBOX (ID,HASH,SCENE,TRACKER,NAME)
108             VALUES ($n, "$i", '', '', ''));
109 0 0         my $rv = $dbh->do($stmt) or die $DBI::errstr;
110 0           $n ++;
111 0           print "INDEX: $n |HASH:\t$i\n";
112             }
113             # Disconnect from database.
114 0           $dbh->disconnect();
115             }
116              
117             sub get_name {
118 0     0 0   my ($s_user, $s_pw, $s_url, $s_port, $s_endp, $s_file) = @_;
119              
120             ## Validate input from ARGV
121 0 0         if (not defined $s_user) { die "USEAGE: Missing server user.\n"; }
  0            
122 0 0         if (not defined $s_pw) { die "USEAGE: Missing server password.\n"; }
  0            
123 0 0         if (not defined $s_url) { die "USEAGE: Missing server url.\n"; }
  0            
124 0 0         if (not defined $s_port) { die "USEAGE: Missing server port.\n"; }
  0            
125 0 0         if (not defined $s_endp) { die "USEAGE: Missing server endpoint.\n"; }
  0            
126 0 0         if (not defined $s_file) { die "USEAGE: Missing server db-filename.\n"; }
  0            
127             # Run Example: perl gen-db.pl user pass host port endpoint
128 0           my $xmlrpc = XML::RPC->new("https://$s_user\:$s_pw\@$s_url\:$s_port\/$s_endp");
129              
130              
131             # Open SQLite database.
132 0           my $driver = "SQLite";
133 0           my $database = "$s_file.db";
134 0           my $dsn = "DBI:$driver:dbname=$database";
135 0           my $userid = ""; # Not implemented no need for database security on local filesystem at this time.
136 0           my $password = ""; # Not implemented.
137 0 0         my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 }) or die $DBI::errstr;
138              
139 0           print "Opened database successfully\n";
140              
141            
142             # Open database and itterate through it.
143 0           my $stmt = qq(SELECT ID, HASH, SCENE, TRACKER, NAME from SEEDBOX;);
144 0           my $sth = $dbh->prepare( $stmt );
145 0 0         my $rv = $sth->execute() or die $DBI::errstr;
146              
147 0 0         if($rv < 0) {
148 0           print $DBI::errstr;
149             }
150              
151 0           while(my @row = $sth->fetchrow_array()) {
152             # Check to see if the NAME value is populated.
153 0 0         if($row[4]) {
154 0           print "ID: ".$row[0]."\tHASH: ".$row[1]."\tSCENE: ".$row[2]."\tTRACKER: ".$row[3]."\tNAME: ".$row[4]."\n";
155             } else {
156             # Get name for specific reccord in the loop.
157 0           my $name = $xmlrpc->call( 'd.get_name',"$row[1]" );
158             # Update reccords.
159 0           my $stmt = qq(UPDATE SEEDBOX set NAME = "$name" where ID=$row[0];);
160 0 0         my $rv = $dbh->do($stmt) or die $DBI::errstr;
161              
162 0 0         if( $rv < 0 ) {
163 0           print $DBI::errstr;
164             } else {
165 0           print "ID: ".$row[0]."\tHASH: ".$row[1]."\tSCENE: ".$row[2]."\tTRACKER: ".$row[3]."\tNAME: ".$name."\n";
166             }
167             }
168             }
169 0           print "Operation done successfully\n";
170              
171             # Disconnect from database.
172 0           $dbh->disconnect();
173             }
174              
175             sub get_tracker {
176 0     0 0   my ($s_user, $s_pw, $s_url, $s_port, $s_endp, $s_file) = @_;
177              
178             ## Validate input from ARGV
179 0 0         if (not defined $s_user) { die "USEAGE: Missing server user.\n"; }
  0            
180 0 0         if (not defined $s_pw) { die "USEAGE: Missing server password.\n"; }
  0            
181 0 0         if (not defined $s_url) { die "USEAGE: Missing server url.\n"; }
  0            
182 0 0         if (not defined $s_port) { die "USEAGE: Missing server port.\n"; }
  0            
183 0 0         if (not defined $s_endp) { die "USEAGE: Missing server endpoint.\n"; }
  0            
184 0 0         if (not defined $s_file) { die "USEAGE: Missing server db-filename.\n"; }
  0            
185             # Run Example: perl gen-db.pl user pass host port endpoint
186 0           my $xmlrpc = XML::RPC->new("https://$s_user\:$s_pw\@$s_url\:$s_port\/$s_endp");
187             # my $dl_list = $xmlrpc->call( 'download_list' );
188              
189             # Open SQLite database.
190 0           my $driver = "SQLite";
191 0           my $database = "$s_file.db";
192 0           my $dsn = "DBI:$driver:dbname=$database";
193 0           my $userid = ""; # Not implemented no need for database security on local filesystem at this time.
194 0           my $password = ""; # Not implemented.
195 0 0         my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 }) or die $DBI::errstr;
196              
197 0           print "Opened database successfully\n";
198              
199            
200             # Open database and itterate through it.
201 0           my $stmt = qq(SELECT ID, HASH, SCENE, TRACKER, NAME from SEEDBOX;);
202 0           my $sth = $dbh->prepare( $stmt );
203 0 0         my $rv = $sth->execute() or die $DBI::errstr;
204              
205 0 0         if($rv < 0) {
206 0           print $DBI::errstr;
207             }
208              
209 0           while(my @row = $sth->fetchrow_array()) {
210             # Check to see if the NAME value is populated.
211 0 0         if($row[3]) {
212 0           print "ID: ".$row[0]."\tHASH: ".$row[1]."\tSCENE: ".$row[2]."\n\tTRACKER: ".$row[3]."\n\tNAME: ".$row[4]."\n";
213             } else {
214             # Get name for specific reccord in the loop.
215 0           my $url = $xmlrpc->call( 't.url',"$row[1]:t0" );
216             #dump($url); # Dump the call for testing purposes.
217             # Update reccords.
218 0           my $stmt = qq(UPDATE SEEDBOX set TRACKER = "$url" where ID=$row[0];);
219 0 0         my $rv = $dbh->do($stmt) or die $DBI::errstr;
220              
221 0 0         if( $rv < 0 ) {
222 0           print $DBI::errstr;
223             } else {
224 0           print "ID: ".$row[0]."\tHASH: ".$row[1]."\tSCENE: ".$row[2]."\n\tTRACKER: ".$url."\n\tNAME: ".$row[4]."\n";
225             }
226             }
227             }
228 0           print "Operation done successfully\n";
229              
230             # Disconnect from database.
231 0           $dbh->disconnect();
232             }
233              
234             sub calc_scene {
235 0     0 0   my ($s_usr, $s_pw, $s_file) = @_;
236              
237 0           print "Active Database: $s_file\n";
238              
239             # Open SQLite database.
240 0           my $driver = "SQLite";
241 0           my $database = "$s_file.db";
242 0           my $dsn = "DBI:$driver:dbname=$database";
243 0           my $userid = ""; # Not implemented no need for database security on local filesystem at this time.
244 0           my $password = ""; # Not implemented.
245 0 0         my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 }) or die $DBI::errstr;
246              
247 0           print "Opened database successfully\n";
248            
249              
250             # Open database and itterate through it.
251 0           my $stmt = qq(SELECT ID, HASH, SCENE, TRACKER, NAME from SEEDBOX;);
252 0           my $sth = $dbh->prepare( $stmt );
253 0 0         my $rv = $sth->execute() or die $DBI::errstr;
254              
255 0 0         if($rv < 0) {
256 0           print $DBI::errstr;
257             }
258              
259 0           while(my @row = $sth->fetchrow_array()) {
260             # Check to see if the NAME value is populated.
261 0           print "\nID: $row[0] :::\n";
262              
263 0 0         if($row[2]) {
264 0           print "\tHASH: ".$row[1]."\tSCENE: ".$row[2]."\n\tTRACKER: ".$row[3]."\n\tNAME: ".$row[4]."\n";
265             } else {
266 0           print "\tDATABSE: Nothing Found! ... Searching the srrdb...\n";
267 0           print "\t * * * SEARCHING * * * : $row[4]";
268 0           my $srrdb_query = qx(srrdb --username=$s_usr --password=$s_pw -s "$row[4]");
269 0           print "\n\tRESULTS: $srrdb_query\n";
270              
271             # Create Database Reccord.
272 0           my $stmt = qq(UPDATE SEEDBOX set SCENE = "$srrdb_query" where ID=$row[0];);
273 0 0         my $rv = $dbh->do($stmt) or die $DBI::errstr;
274 0 0         if( $rv < 0 ) {
275 0           print $DBI::errstr;
276             } else {
277 0           print "\tHASH: ".$row[1]."\tSCENE: ".$srrdb_query."\n\tTRACKER: ".$row[3]."\n\tNAME: ".$row[4]."\n";
278             }
279             }
280              
281 0           print "\t---";
282             }
283 0           print "Operation done successfully\n";
284              
285             # Disconnect from database.
286 0           $dbh->disconnect();
287             }
288              
289             =head1 AUTHOR
290              
291             Clem Morton, C<< >>
292              
293             =head1 BUGS
294              
295             Please report any bugs or feature requests to C, or through
296             the web interface at L. I will be notified, and then you'll
297             automatically be notified of progress on your bug as I make changes.
298              
299             =head1 SUPPORT
300              
301             You can find documentation for this module with the perldoc command.
302              
303             perldoc Rtmgr::Gen::Db
304              
305             You can also look for information at:
306              
307             =over 4
308              
309             =item * RT: CPAN's request tracker (report bugs here)
310              
311             L
312              
313             =item * AnnoCPAN: Annotated CPAN documentation
314              
315             L
316              
317             =item * CPAN Ratings
318              
319             L
320              
321             =item * Search CPAN
322              
323             L
324              
325             =back
326              
327              
328             =head1 ACKNOWLEDGEMENTS
329              
330              
331             =head1 LICENSE AND COPYRIGHT
332              
333             This software is Copyright (c) 2020 by Clem Morton.
334              
335             This is free software, licensed under:
336              
337             The Artistic License 2.0 (GPL Compatible)
338              
339             =cut
340              
341             1; # End of Rtmgr::Gen::Db