-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostfetcher.php
More file actions
128 lines (93 loc) · 3.39 KB
/
postfetcher.php
File metadata and controls
128 lines (93 loc) · 3.39 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
<?php
$username = 'user';
$pass = 'password';
$database = 'db';
$domain = 'host';
$total = 0;
$token = $_GET['token'];
if ($token == '0210d3bb-8c8f-4a54-ab1e-a37589cf14be')
{
mysql_connect($domain, $username, $pass);
@mysql_select_db($database) or die('Failed to connect to database');
$emptyQ = 'TRUNCATE TABLE `posts`';
$query = 'SELECT * FROM `rss feeds`';
mysql_query($emptyQ);
$result = mysql_query($query);
$sub_avg = mysql_result($result, 0);
$num = mysql_num_rows($result);
for ($i=(0); $i<$num; $i++){
unset($doc);
set_time_limit(0);
$feedUrl = mysql_result($result, $i, 'url');
$subscribers = mysql_result($result, $i, 'count');
$feedTitle = mysql_result($result, $i, 'title');
$pubid = mysql_result($result, $i, 'pubid');
$data = file_get_contents($feedUrl);
if ($data == TRUE){
try{
$doc = new SimpleXMLElement($data);
}
catch(exception $e){}
if (!empty($doc)){
$x = $doc->channel->item;
$url = $x->link;
$url = parseUrl($url);
$title = $x->title;
$title = str_replace("\"", "'", $title);
// $title = iconv( "UTF-8", "ISO-8859-1//TRANSLIT", $title );
$title = "\"".$title."\"";
$fql = getFql($url);
$shares = $fql[0];
$likes = $fql[1];
$tCount = getT($url);
$total = $total+$likes+$shares+$tCount;
$insert_query = "INSERT INTO `dailyHedera`.`posts` (`id`, `url`, `pubid`, `title`, `like_count`, `share_count`, `tweet_count`, `rating`, `subscribers`, `tweeted`) VALUES (NULL, '$url', '$pubid', $title, '$likes', '$shares', '$tCount', '0', '$subscribers', false)";
mysql_query($insert_query) or mysql_error();
}
else{
echo "<br>Cannot parse <strong>".$feedTitle."</strong><br>";
}
}
else{
echo '<br><strong>'. $feedTitle."</strong> is not reachable.<br>";
}
}
mysql_Close();
$avg = $total/$num;
echo '<strong>Done!</strong>';
include 'tweeter_ex.php';
}
else{
echo "Permission Denied";
}
// Convert redirect URL to article's real URL and remove query paremeters
function parseUrl($old_url) {
$headers = @get_headers($old_url);
$pattern = '/Location\s*:\s*(https?:[^;\s\n\r]+)/i';
if ($locations = preg_grep($pattern, $headers)) {
preg_match($pattern, end($locations), $redirect);
$old_url = $redirect[1];
}
$parsedURL ='http://'. parse_url($old_url, PHP_URL_HOST).parse_url($old_url, PHP_URL_PATH);
return $parsedURL;
}
// Use Facebook's FQL query api (now outdated) to get the article's share and like count
function getFql($url){
$fql_query = urlencode('SELECT url, share_count, like_count FROM link_stat WHERE url="'.$url.'"');
$fql_url = 'https://api.facebook.com/method/fql.query?query=' . $fql_query;
$result = file_get_contents($fql_url);
$xml = new SimpleXMLElement($result);
$shares = $xml->link_stat->share_count;
$likes = $xml->link_stat->like_count;
$fql = array($shares, $likes);
return $fql;
}
// Use Twitters open URL API that returns an integer representing how many times a specific URL has been tweeted
function getT($url){
$tweetUrl = 'ht'.'tp://urls.api.twitter.com/1/urls/count.json?url=' . $url . '&callback=?';
$JSON = file_get_contents($tweetUrl);
$tData = json_decode($JSON, true);
$tCount = $tData['count'];
return intval($tCount);
}
?>