-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrss2twitter.php
114 lines (86 loc) · 3.29 KB
/
rss2twitter.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
<?php
require_once('twitter.php');
/*
RSS to Twitter using PHP 5.
By: Colin Devroe
http://cdevroe.com/
http://github.com/cdevroe
https://github.com/cdevroe/rss2twitter-PHP5
Originally written on December 6, 2009 while watching
Star Trek III: The Search for Spock.
Version 2.0 - December 29, 2010
See readme.textile for installation instructions, licensing, version history, etc.
*/
/* Setup */
/* Initiate Twitter class
consumerKey, consumerSecret */
$rss2twitter = new Twitter('','');
// Setup OAuthToken,OAuthTokenSecret
$rss2twitter->setOAuthToken('');
$rss2twitter->setOAuthTokenSecret('');
// Feed URL and Cache directory
$feedUrl = 'http://cdevroe.com/feed/'; // e.g. http://cdevroe.com/feed/
$cacheDir = "cache/"; // e.g. /home/.eastwood/domain.com/directory/' Leave blank to turn off caching
// Error reporting level
error_reporting(E_NOTICE); // You may change this for debugging purposes if you'd like.
// Tweet Message format (link is automatically appended)
$tweet_format = '%CATEGORY% "%TITLE%"';
/* End Setup */
// Build the tweet based on the given tweet format
function construct_message($post) {
global $tweet_format;
$message = $tweet_format;
// Replace all variables
$message = str_replace("%CATEGORY%", $post->category[0], $message);
$message = str_replace("%TITLE%", $post->title, $message);
$message = str_replace("%DESCRIPTION%", $post->description, $message);
// Shorten the String if longer than 140 chars incl. 23 char t.co link
if (strlen($message) > 116) {
$message = substr($message, 0, 113).'...';
}
// Append link
$message = $message.': '.$post->link;
return $message;
}
// Retrieve and parse RSS feed
$curl = curl_init($feedUrl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($curl);
curl_close($curl);
// Clean up and parse XML feed
$xml = str_replace("’","'",$xml); $xml = str_replace(" ","",$xml); // Replace ' and nbsp
$parsedXml = new SimpleXMLElement($xml);
// Retrieve and load Cache
if ($cacheDir != '') {
$cacheFile = $cacheDir . "twitterCache.txt"; $timeout = 12; // In hrs.
if ($cached = @file_get_contents($cacheFile)) { $cached = unserialize($cached); }
if (!$cached || !is_array($cached)) { $cached = array_fill(0, 19, "-"); }
}
// Loop through RSS feed items and post to Twitter
foreach ($parsedXml->channel->item as $post) {
// Format pubDate to Unix timestamp
if ($post->pubDate != NULL) { $date = strtotime($post->pubDate); } else { $date = $post->pubDate; }
/* Post to Twitter if:
a. The link has never been tweeted.
b. The post is less than (timeout) hours old.
c. The category is (categoryToTwitter) (optional) */
if (in_array($post->link, $cached) === false && ($date == NULL || $date > time() - (60 * 60 * $timeout))) {
// Construct message
$tweet = construct_message($post);
// Send tweet to Twitter
$sendTweet = $rss2twitter->statusesUpdate($tweet);
print_r($sendTweet);
// If success write to cache, else fail
if (isset($sendTweet['id'])) {
$cached[] = $post->link;
if (count($cached) > 50) { array_shift($cached); }
} else {
echo '<p>There was an error.</p>';
} // End if status */
} // end if (post to twitter)
} // end foreach
// Open, Write, Close, cache.
if ($cacheDir != '') {
$f = fopen($cacheFile,"w+"); fwrite($f,serialize($cached)); fclose($f);
}
?>