Bonjour,

J'ai créé un bot twitter mais celui ci est souvent bloqué car il est détecté en tant que bot donc j'aimerai adapter mon code pour qu'à chaque nouveau tweet il change de clé API (tweet.php Ligne 47-48) et qu'au lieu d'effectuer les requêtes (tweet.php Ligne 50 à 54) une après l'autre (en fait il cherche tout les messages de la première requête puis y répond puis la deuxième recherche ainsi de suite....) qu'il effectue la recherche de la première requêtes puis la deuxième recherche puis une fois arrivé au bout il recommence à la première et ceci 10 fois. Malheureusement je ne sais absolument pas comment faire ça donc si vous pourriez m'aider j'en serai fortement heureux !

Tweet.php:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#!/usr/bin/php
<?php
*
date_default_timezone_set('Europe/Paris');
*
define('DIR', '/var/www/bottwitter/test/');
*
require('TwitterBot.php');
*
header('Content-Type: text/html; charset=utf-8');
*
$random = rand(1,5);
*
switch($random) {
****case 1:
********$consumer = 'Key1';
********$consumerS = 'Key1';
********$token = 'Key1';
********$tokenS = 'Key1';
********break;
****case 2:
********$consumer = 'Key2';
********$consumerS = 'Key2';
********$token = 'Key2';
********$tokenS = 'Key2';
********break;
****case 3:
********$consumer = 'Key3';
********$consumerS = 'Key3';
********$token = 'Key3';
********$tokenS = 'Key3';
********break;
****case 4:
********$consumer = 'Key4';
********$consumerS = 'Key4';
********$token = 'Key4';
********$tokenS = 'Key4';
********break;
****case 5:
********$consumer = 'Key5';
********$consumerS = 'Key5';
********$token = 'Key5';
********$tokenS = 'Key5';
********break;
}
*
$twitter = new TwitterBot($consumer, $consumerS);
$twitter->setToken($token, $tokenS);
*
$twitter->addReply(array('TERME1 -RT'),'TERME1','quelque');
$twitter->addReply(array('TERME2 -RT'),'TERME2','resoud');
$twitter->addReply(array('TERME3 -RT'),'TERME3','soit');
$twitter->addReply(array('TERME4 -RT'),'TERME4','pillier');
$twitter->addReply(array('TERME5 -RT'),'TERME5','portuguais');
*
$twitter->run();
*
?>
Twitterbot.php:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/**
** TwitterBot v1.0
** Benjamin Marquant <https://www.bxnxg.com>
** Twitter API 1.1 via OAuth <https://dev.twitter.com/oauth>
**/
class TwitterBot{
****protected $url_update = 'https://api.twitter.com/1.1/statuses/update.json';
****protected $url_search = 'https://api.twitter.com/1.1/search/tweets.json?q=%s&result_type=recent&count=50&since_id=%s';
****protected $url_verify = 'https://api.twitter.com/1.1/account/verify_credentials.json';
****protected $url_token = 'https://twitter.com/oauth/request_token';
****protected $url_token_access = 'https://twitter.com/oauth/access_token';
****protected $url_auth = 'http://twitter.com/oauth/authorize';
*****
****private $replies = array();
****private $oauth;
****private $screenName;
*****
****public function __construct($key, $secret){
********$this->oauth = new OAuth($key, $secret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);
********$this->oauth->disableSSLChecks();
****}
*****
****public function setToken($token, $secret){
********$this->oauth->setToken($token, $secret);
****}
*****
****public function getSinceId($file='since_id'){
********$file = DIR.$file;
********$since_id = @file_get_contents($file);
********if(!$since_id){
************$since_id = 0;
********}
********return $since_id;
****}
*****
****public function setSinceId($max_id=null,$file='since_id'){
********$file = DIR.$file;
********file_put_contents($file, $max_id);
****}
*****
****private function verifyAccountWorks(){
********try{
************$this->oauth->fetch($this->url_verify, array(), OAUTH_HTTP_METHOD_GET);
************$response = json_decode($this->oauth->getLastResponse());
************$this->screenName = $response->screen_name;
************return true;
********}catch (Exception $ex){
************return false;
********}
****}
*****
****public function test(){
********$array = array(
************'status' => 'Hello World !'
********);
********$this->oauth->fetch($this->url_update, $array, OAUTH_HTTP_METHOD_POST);
****}
*****
****public function addReply($terms,$regex,$type){
********$this->replies[] = array('terms' => $terms,'regex' => $regex,'type' => $type);
****}
*****
****public function run(){
********//echo '========= ', date('Y-m-d g:i:s A'), ' - Started ========='."\n<br>";
********$since_id = $this->getSinceId();
*************
********$max_id = $since_id;
*********
********if ($this->verifyAccountWorks()){
************/* For each request on tweet.php */
************foreach ($this->replies as $key => $t){
****************/* find every tweet since last ID, or the maximum lasts tweets if no since_id */
****************$this->oauth->fetch(sprintf($this->url_search, urlencode($t['terms'][0]), $since_id));
****************$search = json_decode($this->oauth->getLastResponse());
****************if($search){
********************//echo 'Terms #'.$key.' : '.count($search->statuses).' found(s)'."\n<br>";
********************/* Store the last max ID */
********************if ($search->search_metadata->max_id_str > $max_id){
************************$max_id = $search->search_metadata->max_id_str;
********************}
*********************
********************$i = 0;
********************foreach ($search->statuses as $tweet){
************************/* If you are the author of the tweet, we ignore it */
************************if ($tweet->user->screen_name == $this->screenName){
****************************continue;
************************}
************************/* if tweet is a quote (like a RT), we ignore it */
************************if($tweet->is_quote_status){
****************************continue;
************************}
*************************
************************$pass = true;
*****
************************if($pass){
****************************$this->sendReply($tweet, $t);
****************************/* wait 100ms */
****************************usleep(100000);
************************}
********************}
********************//echo 'Terms #'.$key.' : '.$i.' valid(s)'."\n<br>";
****************}
************}
*************
************/* setting new max id */
************$this->setSinceId($max_id);
************//echo '========= ', date('Y-m-d g:i:s A'), ' - Finished ========='."\n<br>";;
********}
****}
*****
****private function sendReply($tweet, $tab, $nodie=false)
****{
********$m3 = ["Réplique 1","Réplique 2 !","Réplique 3","Réplique 4 !","Réplique 5"];
********switch($tab['type']){
************case('quelque'):
****************$reply = $m3[array_rand($m3)]."Réponse 1";
****************break;
************case('resoud'):
****************$reply = $m3[array_rand($m3)]."Réponse 2";
****************break;
************case('soit'):
****************$reply = $m3[array_rand($m3)]."Réponse 3";
****************break;
************case('pillier'):
****************$reply = $m3[array_rand($m3)]."Réponse 4";
****************break;
************case('portuguais'):
****************$reply = $m3[array_rand($m3)]."Réponse 5";
****************break;
************case('STOP'):
****************$reply = 'Je te laisse tranquille. 😏';
****************break;
************case('REVIENT'):
****************$reply = 'Merci, me revoici ! 😏';
****************break;
************default:
****************echo 'ERROR: NO TYPE DEFINED';
****************die();
********}
********try{
************$this->oauth->fetch($this->url_update, array('status' => '@'.$tweet->user->screen_name.' '.$reply,'in_reply_to_status_id' => $tweet->id_str,), OAUTH_HTTP_METHOD_POST);
************sleep(5);
********//* echo $reply;
********}catch(OAuthException $ex){
************echo 'ERROR: '.$ex->lastResponse;
************if(!$nodie){
****************die();
************}
********}
****}
*****
****public function detectSTOP()
****{**
********$since_id = $this->getSinceId("stop_id");
********$max_id = $since_id;
*********
********if (!$this->verifyAccountWorks())
********{
************die();
********}
*********
********$this->oauth->fetch(sprintf($this->url_search, urlencode('stop OR chut OR tg OR relou @pseudo'), $since_id));
********$search = json_decode($this->oauth->getLastResponse());
********$passprincipal = false;
*********
********if($search){
************$stop = json_decode(file_get_contents(DIR.'stop'), true);
************$tweetArray = array();
************$pass = false;
************foreach ($search->statuses as $tweet){
****************if (is_array($stop)) {
********************if(!in_array($tweet->user->screen_name,$stop)){
************************$stop[] = $tweet->user->screen_name;
************************$tweetArray[] = $tweet;
************************$pass = true;
************************$passprincipal = true;
********************}
****************}
************}
************if($passprincipal){
****************file_put_contents(DIR.'stop',json_encode(array_values($stop)));
************}
************if ($search->search_metadata->max_id_str > $max_id){
****************$max_id = $search->search_metadata->max_id_str;
****************$this->setSinceId($max_id,'stop_id');
************}
************if($pass && $tweetArray){
****************foreach ($tweetArray as $tweet){
********************$this->sendReply($tweet, array('type' => 'STOP'), true);
********************usleep(200000);
****************}
************}
********}
****}
*****
****public function detectREVIENT()
****{
********$since_id = $this->getSinceId("revient_id");
********$max_id = $since_id;
*********
********if (!$this->verifyAccountWorks())
********{
************die();
********}
*********
********$this->oauth->fetch(sprintf($this->url_search, urlencode('revient @pseudo'), $since_id));
********$search = json_decode($this->oauth->getLastResponse());
********$passprincipal = false;
*********
********if($search){
************$stop = json_decode(file_get_contents(DIR.'stop'), true);
************$tweetArray = array();
************$pass = false;
************foreach ($search->statuses as $tweet){
****************foreach($stop as $k => &$s){
********************if($s == $tweet->user->screen_name){
************************unset($stop[$k]);
************************$tweetArray[] = $tweet;
************************$pass = true;
************************$passprincipal = true;
********************}
****************}
************}
************if($passprincipal){
****************file_put_contents(DIR.'stop',json_encode(array_values($stop)));
************}
************if ($search->search_metadata->max_id_str > $max_id){
****************$max_id = $search->search_metadata->max_id_str;
****************$this->setSinceId($max_id,'revient_id');
************}
************if($pass && $tweetArray){
****************foreach ($tweetArray as $tweet){
********************$this->sendReply($tweet, array('type' => 'REVIENT'), true);
********************usleep(200000);
****************}
************}
********}
****}
}
?>
Merci d'avance