/
recup_tweet.php
117 lines (113 loc) · 3.74 KB
/
recup_tweet.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
<?php
session_start();
function uni_to_utf ($a){
$a = str_replace('\n' , '' , $a);
$a = str_replace('\/' , '/' , $a);
$a = preg_replace_callback('/\\\\u([0-9a-fA-F]{4})/', function ($match) {
return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UTF-16BE');
}, $a);
return $a;
}
function src_date ($a){
$table = array();
for ($i = 0; $i <= strlen($a); $i++) {
if($a[$i] != ' '){$tmp.=$a[$i];}
else{
array_push($table , $tmp);
$tmp = null;
}
}
array_push($table , $tmp);
$date = $table[5].'-';
switch ($table[1]){
case "Jan": $date .= "01"; break;
case "Feb": $date .= "02"; break;
case "Mar": $date .= "03"; break;
case "Apr": $date .= "04"; break;
case "May": $date .= "05"; break;
case "Jun": $date .= "06"; break;
case "Jul": $date .= "07"; break;
case "Aug": $date .= "08"; break;
case "Sep": $date .= "09"; break;
case "Oct": $date .= "10"; break;
case "Nov": $date .= "11"; break;
case "Dec": $date .= "12"; break;
}
$date .= '-'.$table[2];
return array($date , $table[3] , strtotime($date." ".$table[3]) );
}
function recup_tweet($mc){
require_once('twitter_api/TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "",
'oauth_access_token_secret' => "",
'consumer_key' => "",
'consumer_secret' => ""
);
//$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$url = "https://api.twitter.com/1.1/search/tweets.json";
$requestMethod = "GET";
$getfield = '?q='.$mc.'&lang=fr';
$twitter = new TwitterAPIExchange($settings);
$raw = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$raw = strstr($raw , 'metadata');
$tab_tweet=array();
while( $raw != null ){
$raw = substr($raw , 8);
$tmp = strstr($raw , 'metadata',true);
array_push($tab_tweet , $tmp);
$raw = strstr($raw , 'metadata');
}
unset($tab_tweet[count($tab_tweet)-1]);
unset($tab_tweet[count($tab_tweet)-1]);
//echo count($tab_tweet);
$tab=array();
$j=1;
$bdd = new PDO('mysql:host=localhost;dbname=projet;charset=utf8','root', 'root');
foreach($tab_tweet as $key=>$value){
//Recherche de la date et de l'heure
$tweet = array();
$a = strstr($value , '{"created_at":"');
$tmp = substr($a,15,30);
$date = src_date($tmp);
array_push($tweet , $date);
//Recherche du pseudo
$a = strstr($value , '","location":"', true);
$a = strstr($a , '"screen_name"');
$nom = substr($a,strlen('screen_namee":"'));
array_push($tweet , $nom);
//Recherche du tweet
$a = strstr($value , '","text":"');
$a = strstr($a , '","entities":' , true);
$text = uni_to_utf(substr($a,strlen('","text":"')));
array_push($tweet , $text);
array_push($tab , $tweet);
$where = '`auteur` = "'.$tweet[1].'" and `date` = "'.$tweet[0][0].'" and `heure` = "'.$tweet[0][1].'" and `post` = "'.$tweet[2].'"';
$sql = "SELECT * FROM `post` WHERE ".$where;
$rep = $bdd->query($sql);
$data = $rep->fetch();
if($data == null){
$tweet[2] = utf8_decode(utf8_encode($tweet[2]));
$value = 'VALUES ("'.$tweet[1].'", "'.$tweet[0][0].'", "'.$tweet[0][1].'" ,"'.$tweet[2].'" ,"'.$tweet[0][2].'")';
$sql = "INSERT INTO `post`( `auteur`, `date`, `heure`, `post`, `strtotime`) ".$value;
//echo $sql;
$rep = $bdd->query($sql);
//echo '<br/><br/>';
}
unset($tweet);
$j++;
}
header("Refresh: 1;url='http://localhost/projet/maj2.php'");
}
//recup_tweet('%23Prince');
//recup_tweet($_POST['mc']);
//recup_tweet('Bataclan');
//recup_tweet('LoiTravail');
//recup_tweet('NuitDebout');
//recup_tweet('JoeyStarr');
//recup_tweet('gillesverdez');
//recup_tweet('TeamG1');
?>