-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgdownload.class.php
More file actions
152 lines (107 loc) · 5.24 KB
/
gdownload.class.php
File metadata and controls
152 lines (107 loc) · 5.24 KB
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
<?php
/**
* Package LibUpdater
* Updates github libraries
*/
Class LibUpdater {
public $lib;
public $path;
public $libpath;
public $html;
public $urls;
public $localfolder = '';
public $targetfolder = './tests';
public $foldertree = [];
const GITHUB_URL = 'https://github.com';
const GITHUB_RAW_URL = 'https://raw.githubusercontent.com';
public function __construct( ...$params )
{
// /MaxCDN/php-maxcdn/tree/master/src
if ( count( $params ) == 0 ){
echo "Github project required!" . PHP_EOL;
echo "Usage: $ php gdownload.php \"/githubuser/project/tree/master/some/folder\" /my/target/dir/" . PHP_EOL;
exit;
}
if ( count( $params ) > 0 ) {
$libpath = $params[0];
}
if ( count( $params ) > 1 ) {
$this->targetfolder = $params[1];
}
if ( !is_dir( $this->targetfolder ) ) {
echo 'Target folder is: [' . $this->targetfolder . ']' . PHP_EOL;
mkdir( $this->targetfolder, 0775, true );
} else {
echo 'Target folder [ '. $this->targetfolder . '] already exists...' . PHP_EOL;
}
// Allow HTML5
libxml_use_internal_errors(true);
$this->libpath = $libpath;
$this->path = $this->libpath;
$this->url = self::GITHUB_URL . $this->libpath;
echo 'Loading [' . substr( $this->path, 1, strpos( $this->path, '/', 2 ) - 1 ) . '] from github... (' . self::GITHUB_URL . $this->path . ')' . PHP_EOL;
}
public function parseHTML( $url = null )
{
$url = !empty( $url ) ? $url : $this->url;
$this->lib = file_get_contents( $url );
$doc = new DOMDocument();
$doc->loadHTML( $this->lib );
$this->html = simplexml_import_dom( $doc );
$container = $this->html->body->xpath( '//*[starts-with(@class, "css-truncate")]' );
$this->urls = $container[0]->xpath( '//*[starts-with(@class, "js-navigation-open")]' );
return $this->urls;
}
public function buildFolders( $root_folder )
{
$this->foldertree['remote'][] = $this->url;
$this->foldertree['local'][] = $this->targetfolder . $this->localfolder;
foreach( $root_folder as $key => $folder ) {
if ( ( strpos( $folder[0], '.' ) !== false ) || $folder[0] == '..' ) {
continue;
}
if ( $folder['title'] == 'This path skips through empty directories' ) {
$folder_clean = trim( str_replace( $this->libpath, '', $folder['href'] ), '/' );
$subfolder = explode( '/', $folder_clean );
} else {
$subfolder = (array) $folder['title'][0];
}
$this->foldertree['remote'][] = self::GITHUB_URL . $folder['href'];
$this->localfolder = str_replace( $this->libpath, '', $folder['href'] );
$this->foldertree['local'][] = $this->targetfolder . $this->localfolder;
if ( !is_dir( $this->targetfolder . $this->localfolder ) ) {
echo 'Creating dir ' . $this->targetfolder . $this->localfolder . PHP_EOL;
if ( !mkdir( $this->targetfolder . $this->localfolder, 0775, true ) ) {
echo 'Can\'t mkdir: ' . $this->targetfolder . $this->localfolder . PHP_EOL;
};
} else {
echo 'Directory ' . $this->targetfolder . $this->localfolder . ' already exists, skipping' . PHP_EOL;
}
$this->buildFolders( $this->parseHTML( self::GITHUB_URL . $folder['href'] ) );
}
}
public function downloadFiles()
{
foreach( $this->foldertree['remote'] as $key => $folder ) {
$subfolders = $this->parseHTML( $folder );
foreach( $subfolders as $sub ) {
if ( $sub != '..' ) {
if( strpos( $sub, '.' ) !== false ) {
// Fix URL 1
if ( strpos( $this->foldertree['remote'][$key], '/master' ) === false ) {
$this->foldertree['remote'][$key] .= '/master';
}
// Fix URL 2
$this->foldertree['remote'][$key] = str_replace( self::GITHUB_URL, self::GITHUB_RAW_URL, $this->foldertree['remote'][$key] );
$this->foldertree['remote'][$key] = str_replace( [ 'blob/', 'tree/' ], '', $this->foldertree['remote'][$key] );
// Download file
echo 'Downloading ' . $this->foldertree['remote'][$key] . '/' . $sub . ' to ' . $this->foldertree['local'][$key] . '/' . $sub . PHP_EOL;
$file_contents = file_get_contents( $this->foldertree['remote'][$key] . '/' . $sub );
file_put_contents( $this->foldertree['local'][$key] . '/' . $sub, $file_contents );
}
}
}
}
return true;
}
}