380 lines
15 KiB
PHP
380 lines
15 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Account;
|
|
use App\Models\Concour;
|
|
use App\Models\Contest;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Throwable;
|
|
|
|
class BotController extends Controller
|
|
{
|
|
|
|
public function index()
|
|
{
|
|
|
|
$response = Http::get('https://rtandfollow.com/apibot');
|
|
$tweets = $response->json();
|
|
|
|
$accounts = Account::where('enable', true)->get();
|
|
|
|
foreach($accounts as $account){
|
|
try{
|
|
$user = $account;
|
|
$twitter = new TwitterController();
|
|
$browser = $twitter->connexion($user);
|
|
$url = $browser->driver->getCurrentURL();
|
|
|
|
if($url == 'https://x.com/account/access'){
|
|
$browser->quit();
|
|
exec('taskkill /f /im chrome.exe');
|
|
}else{
|
|
|
|
shuffle($tweets);
|
|
|
|
$a = 1;
|
|
|
|
foreach($tweets as $status) {
|
|
|
|
$tweetid = $status['tweetid'];
|
|
$screen_name = $status['organizer']['screen_name'];
|
|
$text = $status['description'];
|
|
|
|
$concours = DB::table('concours')->where('tweet_id', $tweetid)->where('user', $user->id)->first();
|
|
$blocking = DB::table('blocks')->where('screen_name', $screen_name)->first();
|
|
|
|
$containsBlacklistedWord = preg_match('/' . implode('|', array_map('preg_quote', config('twitter.giveaway_to_blacklist'), ['/'])) . '/i', $text) === 1;
|
|
|
|
//On verifie que le concours n'est pas dans la BDD ou que l'utilisateur n'est pas bloqué
|
|
if (empty($concours) and empty($blocking) and $containsBlacklistedWord !== true) {
|
|
|
|
//On recherche la date de fin du concours
|
|
$fin = $status['fin'];
|
|
|
|
if ($fin >= Carbon::now()->format('Y-m-d')) {
|
|
|
|
try {
|
|
|
|
$tweetspecial = $this->getSpecialComment($text,$tweetid,$screen_name);
|
|
|
|
$tweetcomment = $this->getComment($text);
|
|
|
|
$tags = $this->getTags($text);
|
|
|
|
$hashtags = $this->getHashtags($text);
|
|
|
|
$follows = $this->getFollows($text);
|
|
|
|
$concours = Concour::create([
|
|
'description' => $text,
|
|
'tweet_id' => $tweetid,
|
|
'name' => $screen_name,
|
|
'fin' => $fin,
|
|
'user' => $user->id,
|
|
]);
|
|
|
|
$twitter->retweet($browser, $tweetid);
|
|
|
|
if (isset($tweetspecial) && isset($tags)) {
|
|
$retweet = $tweetspecial . ' ' . $tags . ' ' . $hashtags;
|
|
$encode = urlencode($retweet);
|
|
|
|
//On reply
|
|
$twitter->reply($browser, $tweetid, $encode);
|
|
|
|
}elseif(isset($tweetcomment) && isset($tags)) {
|
|
$comments = config('twitter.sentence_for_tag');
|
|
$comment = Arr::random($comments);
|
|
$retweet = $comment . ' ' . $tags . ' ' . $hashtags;
|
|
$encode = urlencode($retweet);
|
|
|
|
//On reply
|
|
$twitter->reply($browser, $tweetid, $encode);
|
|
|
|
}elseif(isset($tweetspecial)){
|
|
$retweet = $tweetspecial . ' ' . $hashtags;
|
|
$encode = urlencode($retweet);
|
|
|
|
//On reply
|
|
$twitter->reply($browser, $tweetid, $encode);
|
|
|
|
}elseif(isset($tweetcomment)){
|
|
$comments = config('twitter.sentence_for_random_comment');
|
|
$comment = Arr::random($comments);
|
|
$retweet = $comment . ' ' . $hashtags;
|
|
$encode = urlencode($retweet);
|
|
|
|
//On reply
|
|
$twitter->reply($browser, $tweetid, $encode);
|
|
|
|
}elseif(isset($tags)){
|
|
$retweet = $tags;
|
|
$encode = urlencode($retweet);
|
|
|
|
//On reply
|
|
$twitter->reply($browser, $tweetid, $encode);
|
|
}
|
|
|
|
//On follow le créateur
|
|
$twitter->follow($browser, $screen_name);
|
|
|
|
if(isset($follows)){
|
|
foreach ($follows as $follow){
|
|
$twitter->follow($browser, $follow);
|
|
}
|
|
}
|
|
|
|
//On like si besoin
|
|
preg_match("/LIKE/i", $text, $like);
|
|
if (isset($like[0])) {
|
|
$twitter->like($browser, $tweetid);
|
|
}
|
|
|
|
$yes = rand(0,2);
|
|
|
|
if($yes == 1){
|
|
|
|
$new = $this->combinefeeds()[0];
|
|
$title = (strlen($new['title']) > 205) ? substr($new['title'],0,205).'...' : $new['title'];
|
|
//$url = $this->urlshort($new['url']);
|
|
$short = $title.' '.$new['url'];
|
|
$short = urlencode($short);
|
|
//On tweet une info
|
|
$twitter->tweet($browser, $short);
|
|
|
|
}
|
|
|
|
$a++;
|
|
|
|
if ($a > 2) {
|
|
echo $user->name.' a bien participé; ';
|
|
|
|
break;
|
|
}
|
|
|
|
} catch (Throwable $e) {
|
|
|
|
report($e);
|
|
|
|
$a++;
|
|
|
|
if ($a > 2) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (isset($browser) && $browser instanceof Browser) {
|
|
// Récupérez les cookies de la session actuelle
|
|
$cookies = $browser->driver->manage()->getCookies();
|
|
|
|
// Sauvegardez les cookies dans un fichier au format JSON
|
|
$user->cookies = serialize($cookies);
|
|
$user->save();
|
|
|
|
$browser->quit();
|
|
}
|
|
}
|
|
}catch (Throwable $e) {
|
|
report($e);
|
|
}
|
|
}
|
|
|
|
exec('taskkill /f /im chrome.exe');
|
|
|
|
}
|
|
|
|
public function process($user,$id){
|
|
$user = Account::find($user);
|
|
|
|
$contest = Contest::findOrFail($id);
|
|
|
|
$text = $contest->description;
|
|
|
|
$tweetspecial = $this->getSpecialComment($text,$user,$contest->tweetid);
|
|
|
|
$tweetcomment = $this->getComment($text);
|
|
|
|
$tags = $this->getTags($text);
|
|
|
|
$hashtags = $this->getHashtags($text);
|
|
|
|
dd($tweetspecial,$tweetcomment,$tags,$hashtags);
|
|
}
|
|
|
|
private function getSpecialComment($text,$user,$id){
|
|
|
|
//On recherche si une reponse special est attendu
|
|
$word_special_comments = config('twitter.word_special_comment');
|
|
$combined_regex = implode('|', array_map('preg_quote', $word_special_comments, array_fill(0, count($word_special_comments), '/')));
|
|
if (preg_match('/' . $combined_regex . '/i', $text, $matches)) {
|
|
try {
|
|
$API = new APIController();
|
|
|
|
// On check les notifs
|
|
$texts = $API->getweets($user, $id);
|
|
|
|
// Filtrer les phrases vides
|
|
$texts = array_filter($texts, function($phrase) {
|
|
return !empty(trim($phrase)); // Ignore les chaînes vides ou contenant uniquement des espaces
|
|
});
|
|
|
|
// Initialiser un tableau pour stocker les occurrences des phrases
|
|
$occurrences = [];
|
|
|
|
foreach ($texts as $index1 => $phrase1) {
|
|
foreach ($texts as $index2 => $phrase2) {
|
|
if ($index1 !== $index2) {
|
|
$similarity = $this->cosineSimilarity($phrase1, $phrase2);
|
|
// Vous pouvez ajuster le seuil de similarité en fonction de vos besoins
|
|
if ($similarity > 0.5) {
|
|
// Incrémenter le compteur pour les deux phrases
|
|
$occurrences[$index1] = isset($occurrences[$index1]) ? $occurrences[$index1] + 1 : 1;
|
|
$occurrences[$index2] = isset($occurrences[$index2]) ? $occurrences[$index2] + 1 : 1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Trouver l'index de la phrase avec le comptage le plus élevé
|
|
$indexPhrasePlusFrequente = (!empty($occurrences)) ? array_search(max($occurrences), $occurrences) : null;
|
|
|
|
// Récupérer la phrase avec le comptage le plus élevé
|
|
$phrasePlusFrequente = ($indexPhrasePlusFrequente !== null) ? $texts[$indexPhrasePlusFrequente] : null;
|
|
|
|
if ($phrasePlusFrequente != null) {
|
|
// Supprimer les emojis
|
|
$phrasePlusFrequente = $this->remove_emojis($phrasePlusFrequente);
|
|
|
|
return $phrasePlusFrequente;
|
|
} else {
|
|
$tweetcomments = config('twitter.sentence_for_random_comment');
|
|
return Arr::random($tweetcomments);
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
$tweetcomments = config('twitter.sentence_for_random_comment');
|
|
return Arr::random($tweetcomments);
|
|
}
|
|
}
|
|
}
|
|
private function getComment($text){
|
|
//On recherche si un commentaire est attendu
|
|
$word_comments = config('twitter.word_comment');
|
|
$combined_regex = implode('|', array_map('preg_quote', $word_comments, array_fill(0, count($word_comments), '/')));
|
|
if (preg_match('/' . $combined_regex . '/i', $text, $matches)) {
|
|
return true;
|
|
}
|
|
}
|
|
private function getTags($text){
|
|
//On recherche si ça demande un tag
|
|
$word_tags = config('twitter.word_tag');
|
|
$combined_regex = implode('|', array_map('preg_quote', $word_tags, array_fill(0, count($word_tags), '/')));
|
|
$tags = collect(config('twitter.tags'));
|
|
if (preg_match('/' . $combined_regex . '/i', $text, $matches)) {
|
|
|
|
//On recherche si ça demande 1 tag
|
|
$one_people_lists = config('twitter.one_people_list');
|
|
$combined_regex = implode('|', array_map('preg_quote', $one_people_lists, array_fill(0, count($one_people_lists), '/')));
|
|
if (preg_match('/' . $combined_regex . '/i', $text, $matches)) {
|
|
$values = $tags->random(1);
|
|
return '@' . $values[0];
|
|
}
|
|
|
|
//On recherche si ça demande 2 tags
|
|
$two_people_lists = config('twitter.two_people_list');
|
|
$combined_regex = implode('|', array_map('preg_quote', $two_people_lists, array_fill(0, count($two_people_lists), '/')));
|
|
if (preg_match('/' . $combined_regex . '/i', $text, $matches)) {
|
|
$values = $tags->random(2);
|
|
return '@' . $values[0]. ' @' . $values[1];
|
|
}
|
|
|
|
//On recherche si ça demande 3 tags ou plus
|
|
$three_or_more_people_lists = config('twitter.three_or_more_people_list');
|
|
$combined_regex = implode('|', array_map('preg_quote', $three_or_more_people_lists, array_fill(0, count($three_or_more_people_lists), '/')));
|
|
if (preg_match('/' . $combined_regex . '/i', $text, $matches)) {
|
|
$values = $tags->random(3);
|
|
return '@' . $values[0]. ' @' . $values[1]. ' @' . $values[2];
|
|
}
|
|
}
|
|
}
|
|
private function getHashtags($text){
|
|
preg_match_all("/#[a-zA-Z0-9]+/", $text, $hashtags);
|
|
|
|
if (isset($hashtags[0])) {
|
|
$hashtags = array_unique($hashtags[0]);
|
|
$blacklist = config('twitter.hashtag_to_blacklist');
|
|
|
|
// Comparer les deux tableaux et supprimer les correspondances du premier tableau
|
|
$resultLower = array_udiff($hashtags, $blacklist, 'strcasecmp');
|
|
|
|
// Construire la chaîne finale des hashtags restants
|
|
$final = implode(" ", $resultLower);
|
|
|
|
return $final;
|
|
}
|
|
else{
|
|
return '';
|
|
}
|
|
}
|
|
private function cosineSimilarity($text1, $text2)
|
|
{
|
|
$words1 = str_word_count(strtolower($text1), 1);
|
|
$words2 = str_word_count(strtolower($text2), 1);
|
|
|
|
$allWords = array_unique(array_merge($words1, $words2));
|
|
$vector1 = $vector2 = [];
|
|
|
|
// Construire les vecteurs avec les fréquences des mots
|
|
foreach ($allWords as $word) {
|
|
$vector1[] = in_array($word, $words1) ? 1 : 0;
|
|
$vector2[] = in_array($word, $words2) ? 1 : 0;
|
|
}
|
|
|
|
$dotProduct = 0;
|
|
|
|
// Calculer le produit scalaire des vecteurs
|
|
for ($i = 0; $i < count($allWords); $i++) {
|
|
$dotProduct += $vector1[$i] * $vector2[$i];
|
|
}
|
|
|
|
$magnitude1 = sqrt(array_sum($vector1));
|
|
$magnitude2 = sqrt(array_sum($vector2));
|
|
|
|
if ($magnitude1 * $magnitude2 == 0) {
|
|
return 0; // Pour éviter une division par zéro
|
|
}
|
|
|
|
return $dotProduct / ($magnitude1 * $magnitude2);
|
|
}
|
|
private function getFollows(mixed $text)
|
|
{
|
|
preg_match_all("/\s@([\w_-]+)/", $text, $mentions);
|
|
|
|
if(isset($mentions[1])){
|
|
$mentions = array_unique($mentions[1]);
|
|
return $mentions;
|
|
|
|
}else{
|
|
return '';
|
|
}
|
|
}
|
|
private function remove_emojis($string)
|
|
{
|
|
// Match all emojis (including extended ones)
|
|
$regex_emojis = '/[\x{1F600}-\x{1F64F}\x{1F300}-\x{1F5FF}\x{1F680}-\x{1F6FF}\x{1F900}-\x{1F9FF}\x{2600}-\x{26FF}\x{2700}-\x{27BF}]/u';
|
|
$clear_string = preg_replace($regex_emojis, '', $string);
|
|
|
|
return $clear_string;
|
|
}
|
|
|
|
|
|
|
|
}
|