-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget.php
143 lines (118 loc) · 3.93 KB
/
get.php
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
<?php
error_reporting( E_ALL ); // Since this is just a utility script.
require_once( './database.php' );
$sql = "SELECT value FROM `config` WHERE `key`='extsToTry'";
$result = verify_query( $sql );
$row = mysql_fetch_assoc($result);
$extsToTry = explode( ',', $row[ 'value' ] );
mysql_free_result( $result );
print_r( $extsToTry );
function failure( $msg='' )
{
echo "<h1>Failure</h1><br />\n";
if ( !empty( $msg ) )
echo $msg;
}
/*
Function: grabComics
Grabs comics in a given directory starting with 00001.png and working on up.
Parameters:
grab_uri - The URI 'directory' to grab from.
output_uri - The directory to copy to.
*/
function grabComics( $grab_uri, $output_uri )
{
$num = 0;
$fetched = 0; // How many we actually retreived
// create a cURL handle
$ch = curl_init();
curl_setopt( $ch, CURLOPT_BINARYTRANSFER, TRUE ); // We're grabbing images
curl_setopt( $ch, CURLOPT_FILETIME, TRUE ); // We want the modify date
curl_setopt( $ch, CURLOPT_HEADER, FALSE ); // We don't want headers with the output
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE ); // Don't output return
// First, define our vars
$info = NULL;
$filename = NULL;
global $extsToTry;
global $maxSkip;
$curSkip = 0;
$sql = "SELECT value FROM `config` WHERE `key`='extsToTry'";
$result = verify_query( $sql );
$row = mysql_fetch_assoc($result);
$extsToTry = explode( ',', $row[ 'value' ] );
mysql_free_result( $result );
$sql = "SELECT value FROM `config` WHERE `key`='maxSkip'";
$result = verify_query( $sql );
$row = mysql_fetch_assoc($result);
$maxSkip = $row[ 'value' ];
mysql_free_result( $result );
while ( true ) { // Break inside
// First we'll check if we have this file already
$alreadyFetched = FALSE;
foreach ( $extsToTry as $ext ) {
$filename = sprintf( './%05d.%s', $num, $ext );
if ( file_exists( $output_uri . $filename ) ) {
$alreadyFetched = TRUE;
break;
}
}
if ( $alreadyFetched === TRUE ) {
$num++;
$curSkip = 0;
continue;
}
// Now to fetch it from the site
foreach ( $extsToTry as $ext ) {
$filename = sprintf( './%05d.%s', $num, $ext );
curl_setopt( $ch, CURLOPT_URL, $grab_uri . $filename ); // The URL of course
$ret = curl_exec( $ch );
$info = curl_getinfo( $ch );
if ( $info[ 'http_code' ] == 200 )
break; // Found it on this ext
}
if ( $info[ 'http_code' ] == 404 ) {
if ( $curSkip <= $maxSkip ) {
$curSkip++;
$num++;
continue; // Goto next
} else { // Else we've hit the end
$num -= $curSkip + 1;
echo "<h1>Success</h1><br />\nFetched ${fetched} comics for ${grab_uri}. There are ${num} comics for this series now.<br />\n";
break;
}
} elseif ( $info[ 'http_code' ] != 200 ) {
failure( "Received http code " . $info[ 'http_code' ] . " for ${grab_uri}.<br />\n" );
break;
}
$curSkip = 0;
$fh = fopen( $output_uri . $filename, "w" );
if ( $fh === FALSE ) {
failure( "Unable to open output file ${output_uri}${filename}.<br />\n" );
break;
}
$bytes = fwrite( $fh, $ret );
fclose( $fh );
if ( $bytes == FALSE ) {
failure( "Error writing file ${output_uri}${filename}.<br />\n" );
break;
}
$success = touch( $output_uri . $filename, intval( $info[ 'filetime' ] ) ); // Set modified date
if ( $success === FALSE ) {
echo "WARNING: Failed to touch file ${output_uri}${filename}.<br />\n";
}
echo "Successfully wrote ${filename}.<br />\n";
$num++;
$fetched++;
}
// close cURL resource, and free up system resources
curl_close( $ch );
}
grabComics( "http://www.rhjunior.com/QQSR/Images/", "./QQSR/" );
//grabComics( "http://rhjunior.com/totq/Images/", "./totq/" );
//grabComics( "http://rhjunior.com/NT/Images/", "./NT/" );
//grabComics( "http://rhjunior.com/GH/Images/", "./GH/" );
//grabComics( "http://rhjunior.com/TH/Images/", "./TH/" );
//grabComics( "http://rhjunior.com/FoH/Images/", "./FoH/" );
//grabComics( "http://rhjunior.com/CC/Images/", "./CC/" );
mysql_close( $link );
?>