-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplustotweet.php
88 lines (77 loc) · 3.18 KB
/
plustotweet.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
<?php
/**
* Run this script in the background and it will poll your Google+ feed for public posts every 5-10 minutes.
* If it finds any new posts it will post them to Twitter.
*
*/
require_once('tmhOAuth/tmhOAuth.php');
$google_userid = "<your_google_userid>";
$google_key = "<your_google_api_key>";
$twitter_consumerKey = "<your_twitter_consumerKey>";
$twitter_consumerSecret = "<your_twitter_consumerSecret>";
$twitter_oauthToken = "<your_twitter_oauthToken>";
$twitter_oauthSecret = "<your_twitter_oauthSecret>";
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => $twitter_consumerKey,
'consumer_secret' => $twitter_consumerSecret,
'user_token' => $twitter_oauthToken,
'user_secret' => $twitter_oauthSecret,
));
$max_results = 5;
$plus_filter = "items(title,verb,url,published,object/attachments(objectType,content,fullImage/url))";
$checktime = (int)(date('U', time()));
$imagename = "upload.jpg";
while (true) {
$request = @file_get_contents("https://www.googleapis.com/plus/v1/people/$google_userid/activities/public?key=$google_key&maxResults=$max_results&fields=$plus_filter");
$obj = json_decode($request);
echo "Previous check: ".date('r', $checktime)."\n";
foreach ($obj->{'items'} as $item) {
@unlink($imagename);
$image = False;
$title = $item->{'title'};
$verb = $item->{'verb'};
$url = $item->{'url'};
$attachments = (empty($item->{'object'}->{'attachments'})) ? array() : $item->{'object'}->{'attachments'};
$published = $date = date('U', strtotime($item->{'published'}));
if ($published < $checktime) {
echo "Not a new post. Skipping.\n";
}
else {
if ($title != "" and $verb != "share" ) {
$shorturl = @file_get_contents("http://hyv.es/api/?url=$url");
foreach ($attachments as $attachment) {
$a_type = $attachment->{'objectType'};
$a_content = $attachment->{'content'};
$a_url = $attachment->{'fullImage'}->{'url'};
if ( $a_type == "photo" and $a_content != "" ) {
$image = True;
file_put_contents($imagename, file_get_contents($a_url));
break;
}
}
$status = "$title $shorturl";
echo "Posting to Twitter: $status\n";
if ($image) {
$tmhOAuth->request('POST', 'https://upload.twitter.com/1/statuses/update_with_media.json',
array(
'media[]' => "@{$imagename}",
'status' => $status
),
true, // use auth
true // multipart
);
}
else {
$tmhOAuth->request('POST', $tmhOAuth->url('1/statuses/update'), array(
'status' => $status
));
}
}
}
}
$checktime = (int)(date('U', time()));
$sleeptime = rand ( 300, 600);
echo "Sleeping for $sleeptime seconds\n\n";
sleep($sleeptime);
}
?>