This repository has been archived by the owner on Dec 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.php
124 lines (114 loc) · 3.62 KB
/
common.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
115
116
117
118
119
120
121
122
123
124
<?php
/**
* Convert tweets from English to Piratespeak
* Copyright (C) 2009 Anirudh Surendranath
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
**/
function theme_tweets($tweets,$username) {
$output .= '<div class="tweets">';
$counter = 1;
foreach($tweets as $tweet) {
if(stripos($tweet,$username.': ')===0) {
$tweet = substr($tweet,strlen($username.': '));
}
$odd_even_class = ($counter%2) ? 'tweet-odd' : 'tweet-even';
$output .= ' <p class="tweet '.$odd_even_class.'" >'.$tweet.'</p>';
$counter++;
}
$output .= '</div>';
return $output;
}
// SELECT * FROM piratespeak.translate WHERE html IN (SELECT text from url = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=gingerjoos&count=10&include_rts=true")
//
//
function get_pirate_tweets($username) {
$tweet_texts = fetch_tweets($username);
if(!$tweet_texts) {
return FALSE;
}
$themed_content = theme_tweets($tweet_texts,$username);
return translate_to_piratespeak($themed_content);
}
function fetch_url($url,$params,$method='GET') {
if($method == 'GET')
$url = $url . '?' . http_build_query($params);
$ch = curl_init();
curl_setopt($ch,CURLOPT_TIMEOUT,YQL_FETCH_TIMEOUT);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_FAILONERROR,FALSE);
curl_setopt($ch,CURLOPT_URL,$url);
$result = curl_exec($ch);
$status_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_close($ch);
return array(
'status_code' => $status_code,
'result' => $result,
);
}
function translate_to_piratespeak($content) {
$url = build_yql_url($content);
$api_url = $url['api_url'];
$params = $url['params'];
$response = fetch_url($api_url,$params);
if($response['status_code'] != '200')
return FALSE;
var_dump($response['result']);exit;
return $response['result'];
}
function fetch_tweets($username) {
$api_url = 'https://api.twitter.com/1/statuses/user_timeline.json';
$params = array(
'screen_name' => $username,
'include_rts' => 'true',
);
$response = fetch_url($api_url,$params);
if($response['status_code'] != '200')
return FALSE;
$tweets = json_decode($response['result']);
$tweet_text = array();
foreach($tweets as $tweet) {
$tweet_text[] = $tweet->text;
}
return $tweet_text;
}
function build_yql_url($content) {
$api_url = 'http://query.yahooapis.com/v1/public/yql?';
$query = build_query($content);
$params = array (
'q' => $query,
'format' => 'json',
'env' => 'store://kid666.com/piratespeak',
);
return compact('api_url','params');
}
function build_query($content) {
$query = 'SELECT * FROM piratespeak.translate WHERE html = "' . htmlentities($content) . '"';
return $query;
}
function get_default_tweetname() {
$username = strip_tags($_GET['tweetname']);
if(!empty($username)) {
return $username;
}
else if(defined('DEFAULT_TWEETNAME')) {
return DEFAULT_TWEETNAME;
}
else {
return 'Yer twitter username';
}
}
// vim: tabstop=2 softtabstop=2 shiftwidth=2 expandtab ai