This repository was archived by the owner on May 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpi.tweets.php
More file actions
162 lines (138 loc) · 4.48 KB
/
pi.tweets.php
File metadata and controls
162 lines (138 loc) · 4.48 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
153
154
155
156
157
158
159
160
161
162
<?php
class Plugin_tweets extends Plugin {
var $meta = array(
'name' => 'Tweets',
'version' => '1.0',
'author' => 'Alex Glover',
'author_url' => 'http://alex-glover.com'
);
public $username;
public $count;
public $include_retweets;
public $exclude_replies;
public $include_entities;
/**
* Displays the tweets
*
* <code>
* {{ tweets: display username="{{username}}" count="5" include_rewtweets="false" date_format="Y-m-d" }}
* {{ /tweets:display }}
* </code>
*/
public function display()
{
$this->username = $this->fetch_param('username');
$this->count = $this->fetch_param('count', 3, 'is_numeric'); # defaults to 3, validates as numeric
$this->include_retweets = $this->fetch_param('include_retweets', true, false, true); # defaults to true, always returns boolean
$this->exclude_replies = $this->fetch_param('exclude_replies', false, false, true); #defaults to false, always returns boolean
$this->include_entities = $this->fetch_param('include_entities', true, false, true); # defaults to true, always returns boolean
$cache = $this->fetch_param('cache', false, false, true); # defaults to false
$cache_expire = $this->fetch_param('cache_expire', 10, 'is_numeric');
// get the tagdata
$content = $this->content;
// if username is empty, bail out
if(!$this->username)
{
// @todo better bailing out
return false;
}
if($cache)
{
// check for the cache
if(file_exists('_cache/twitter_cache'))
{
// if cache is expired
$cache_expire_time = filemtime('_cache/twitter_cache') + ((int) $cache_expire * 60);
if(time() > $cache_expire_time)
{
$tweets = $this->get_tweets();
}
else
{
// get the tweets from the file
$tweets = json_decode(file_get_contents('_cache/twitter_cache'), true);
}
}
else
{
$this->get_tweets();
}
}
else
{
$tweets = $this->get_tweets();
}
// is this hacky? I am just reformatting some of the data
foreach($tweets as $index => $tweet)
{
$tweets[$index]['time_ago'] = $this->time_ago($tweet['created_at']);
$tweets[$index]['text'] = $this->convert_links($tweet['text']);
//$content = $this->parse_loop($content, $tweet['user']);
}
return $this->parse_loop($content, $tweets);
}
private function get_tweets()
{
// create twitter REST API url
// @link https://dev.twitter.com/docs/api/1/get/statuses/user_timeline
$uri = "https://api.twitter.com/1/statuses/user_timeline.json";
$config = array(
'screen_name' => $this->username,
'count' => $this->count,
'include_rts' => $this->include_retweets,
'exclude_replies' => $this->exclude_replies,
'include_entities' => $this->include_entities,
);
// create the request uri for twitter
$request_uri = $uri . '?' . http_build_query($config);
$twitter_json = file_get_contents($request_uri);
$tweets = json_decode($twitter_json, true);
file_put_contents('_cache/twitter_cache', $twitter_json);
return $tweets;
}
/**
* Calculates the time ago
*
* @return String $timeago
*/
private function time_ago($date, $granularity = 2)
{
$date = strtotime($date);
$difference = time() - $date;
$return = "";
$periods = array(
'decade' => 315360000,
'year' => 31536000,
'month' => 2628000,
'week' => 604800,
'day' => 86400,
'hour' => 3600,
'minute' => 60,
'second' => 1
);
foreach ($periods as $key => $value)
{
if ($difference >= $value)
{
$time = floor($difference/$value);
$difference %= $value;
$return .= ($return ? ' ' : '') . $time . ' ';
$return .= (($time > 1) ? $key.'s' : $key);
$granularity--;
}
if ($granularity == '0') { break; }
}
return $return;
}
/**
* Converts any twitter links into clickable links
*/
private function convert_links($text)
{
$text = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $text);
$text = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $text);
$text = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $text);
$text = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $text);
return $text;
}
}