/
new_photo_detect.php
87 lines (76 loc) · 2.46 KB
/
new_photo_detect.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
<?php
set_time_limit(0);
require 'libs/LIB_http.php';
require 'libs/LIB_download_images.php';
require "libs/phpFlickr.php";
define("host", "mysql:host=localhost;dbname=your-db-name");
define("user_name", "your-name");
define("user_pwd", "your-password");
//Using curl libs http get url.
function get_paging($url)
{
$result = http_get($url, $refer = "");
$result = $result["FILE"];
$result = json_decode($result, true);
return $result;
}
//Using flickr api to upload pics.
function upload_photo($path, $title)
{
$apiKey = "your-flickr-key";
$apiSecret = "your-flickr-secret";
$token = "flickr-token";
$permissions = "write";
$f = new phpFlickr($apiKey, $apiSecret, true);
$f->setToken($token);
$f->async_upload($path, $title);
@unlink("temp.jpg");
}
function save_binary_file($url)
{
$binary_file = download_binary_file($url, $refer = "");
file_put_contents("temp.jpg", $binary_file);
}
$link = new PDO(host, user_name, user_pwd);
$link -> query("SET NAMES utf8");
//using while.... until next link data is empty.
$result = get_paging("https://graph.facebook.com/1450930895146846/feed?access_token=your-access-token&limit=250");
$data_arr = $result["data"];
$data_arr_len = count($data_arr);
for($count=0;$count<$data_arr_len;$count++)
{
$message = $data_arr[$count]["message"];
$object_id = $data_arr[$count]["object_id"];
$sql = "SELECT COUNT(obj_id) FROM beauty WHERE obj_id = :object_id";
$stmt = $link -> prepare($sql);
$stmt -> execute(array(":object_id"=>$object_id));
if($stmt -> fetchColumn() !== 0)
break;
if(mb_stristr($message, "正妹") === false)
continue;
save_binary_file("https://graph.facebook.com/".$object_id."/picture?type=normal");
upload_photo("temp.jpg", $message);
save_binary_file("https://graph.facebook.com/".$object_id."/picture?type=thumbnail");
upload_photo("temp.jpg", $message);
$sql = "INSERT INTO beauty(message,obj_id) VALUES(:message,:obj_id)";
try
{
$stmt = $link -> prepare($sql);
$stmt -> execute(array(":message"=>$message, ":obj_id"=>$object_id,));
}
catch (PDOException $e)
{
//check duplicate entries.
if($e->errorInfo[0] == '23000' && $e->errorInfo[1] == '1062')
{
if(file_exists("error_log.log"))
@unlink("error_log.log");
file_put_contents("error_log.log", "Duplicate data\r\n");
}
else
{
file_put_contents("error_log.log", "Other errors.\r\n");
}
}
}
?>