-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShutterstockFiles.php
94 lines (84 loc) · 3.06 KB
/
ShutterstockFiles.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
<?php
class ShutterstockFiles
{
private function file_get_contents_curl($url) {
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,$url);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Your application name');
$query = curl_exec($curl_handle);
curl_close($curl_handle);
return $query;
}
private function Waiter($results, $folder){
echo "Waiting fot workers...Shutter Portfolio\n";
do{
$flag = "yes";
foreach ($results as $page) {
if(!(file_exists("./".$folder."/ShutterFiles/".$page.".txt"))){
$flag = "no";
break;
}
}
}while($flag == "no");
}
private function UniUrl($url){
$urlNew = preg_replace("/.\.html/" , "", $url);
return $urlNew;
}
private function GetCountOfFiles($portfolio){
$portfolio = $this->file_get_contents_curl($portfolio);
preg_match_all('/id="pf_num_images">.*<\/li>/', $portfolio, $count1);
preg_match('/\d+/', $count1[0][0], $count);
$count = (int)$count[0];
return $count;
}
private function ParseAll($results, $folder){
echo "Parsing files...Shutter Portfolio\n";
$array = array();
foreach ($results as $page) {
$file = json_decode(file_get_contents("./".$folder."/ShutterFiles/".$page.".txt"), true);
foreach ($file as $value) {
$array[] = $value;
}
}
return $array;
}
private function WorkersTime($PortfolioURL, $countFiles, $folder){
$i = 1 ;
$arrayShutter = array();
$countPages = (int)($countFiles/100)+1;
while ($i <= $countPages) {
$results = array();
for ($ii = 0; $ii < 300; $ii++){
$results[]= $i;
exec("php workerShutterFiles.php ".$PortfolioURL.$i.".html ".$i." ".$folder." >> /dev/null &");
$i++;
if($i > $countPages){break;}
}
$this->Waiter($results, $folder);
$arrayShutter = array_merge($arrayShutter , $this->ParseAll($results, $folder));
}
exec("rm ./".$folder."/ShutterFiles/*.txt");
return $arrayShutter;
}
private function NormalizationShutterArray($arrayShutter){
$array = array();
foreach ($arrayShutter as $value) {
preg_match("/-\d+\.jpg/", $value, $arr);
$key = preg_replace(array("/-/" , "/\.jpg/"), "", $arr[0]);
$array[] = array("thumb" => $value, "id" => $key);
}
return $array;
}
public function ShutterFiles($PortfolioURL, $folder){
$countFiles = $this->GetCountOfFiles($PortfolioURL);
$PortfolioURL = $this->UniUrl($PortfolioURL);
$arrayShutter = $this->WorkersTime($PortfolioURL, $countFiles, $folder);
$arrayShutter = $this->NormalizationShutterArray($arrayShutter);
return $arrayShutter;
}
}
// $script = new script();
// var_dump($script->ShutterFiles("http://www.shutterstock.com/gallery-176509p1.html"));