354 lines
12 KiB
PHP
354 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Http\Controllers\APIController;
|
|
use App\Models\Account;
|
|
use App\Models\Contest;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Queue\Queueable;
|
|
use Illuminate\Support\Arr;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class ProcessTweet implements ShouldQueue
|
|
{
|
|
use Queueable;
|
|
|
|
public $timeout = 90;
|
|
public $tries = 1;
|
|
|
|
private $id;
|
|
|
|
private $authid;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*/
|
|
public function __construct($id,$authid)
|
|
{
|
|
$this->id = $id;
|
|
|
|
$this->authid= $authid;
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*/
|
|
public function handle(): void
|
|
{
|
|
|
|
try{
|
|
$user = Account::find($this->authid);
|
|
|
|
$contest = Contest::findOrFail($this->id);
|
|
|
|
$text = $contest->description;
|
|
|
|
$tweetspecial = $this->getSpecialComment($text,$user,$contest->tweetid);
|
|
|
|
$tweetcomment = $this->getComment($text);
|
|
|
|
$tags = $this->getTags($text);
|
|
|
|
$hashtags = $this->getHashtags($text);
|
|
|
|
$follows = $this->getFollows($text);
|
|
|
|
$API = new APIController();
|
|
|
|
//On check les notifs
|
|
$API->unread($user);
|
|
|
|
//On Reweet
|
|
$API->retweet($user, $contest->tweetid);
|
|
sleep(15);
|
|
|
|
|
|
if (isset($tweetspecial) && isset($tags)) {
|
|
$retweet = $tweetspecial . ' ' . $tags . ' ' . $hashtags;
|
|
|
|
//On reply
|
|
$API->reply($user, $contest->tweetid, $retweet);
|
|
sleep(15);
|
|
|
|
}elseif(isset($tweetcomment) && isset($tags)) {
|
|
$comments = config('twitter.sentence_for_tag');
|
|
$comment = Arr::random($comments);
|
|
$retweet = $comment . ' ' . $tags . ' ' . $hashtags;
|
|
|
|
//On reply
|
|
$API->reply($user, $contest->tweetid, $retweet);
|
|
sleep(15);
|
|
|
|
|
|
}elseif(isset($tweetspecial)){
|
|
$retweet = $tweetspecial . ' ' . $hashtags;
|
|
|
|
//On reply
|
|
$API->reply($user, $contest->tweetid, $retweet);
|
|
sleep(15);
|
|
|
|
|
|
}elseif(isset($tweetcomment)){
|
|
$comments = config('twitter.sentence_for_random_comment');
|
|
$comment = Arr::random($comments);
|
|
$retweet = $comment . ' ' . $hashtags;
|
|
|
|
//On reply
|
|
$API->reply($user, $contest->tweetid, $retweet);
|
|
sleep(15);
|
|
|
|
|
|
}elseif(isset($tags)){
|
|
$retweet = $tags;
|
|
|
|
//On reply
|
|
$API->reply($user, $contest->tweetid, $retweet);
|
|
sleep(15);
|
|
|
|
}
|
|
|
|
//On follow le créateur
|
|
$API->follow($user, $contest->screen);
|
|
sleep(15);
|
|
|
|
|
|
if(isset($follows)){
|
|
foreach ($follows as $follow){
|
|
//On folow les personnes demandées
|
|
$API->follow($user, $follow);
|
|
sleep(5);
|
|
}
|
|
}
|
|
|
|
//On like si besoin
|
|
preg_match("/LIKE/i", $text, $like);
|
|
if (isset($like[0])) {
|
|
//On like le tweet
|
|
$API->like($user, $contest->tweetid);
|
|
sleep(15);
|
|
}
|
|
|
|
$nb = rand(3,5);
|
|
|
|
if (Cache::has('news')) {
|
|
$news = Cache::get('news');
|
|
}else{
|
|
$news = $API->newstweet($user);
|
|
}
|
|
|
|
shuffle($news);
|
|
|
|
$nb = rand(3,5);
|
|
|
|
if (count($news) >= $nb) {
|
|
$selectedArticles = array_slice($news, 0, $nb);
|
|
|
|
foreach ($selectedArticles as $article) {
|
|
$tweetid = $article['conversation_id_str'];
|
|
$API->retweet($user, $tweetid);
|
|
sleep(15);
|
|
}
|
|
}
|
|
|
|
$contest->increment('count');
|
|
|
|
return;
|
|
|
|
}catch (Exception $exception){
|
|
$text = "Le compte Twitter " . $user->name . " : " . $exception->getMessage();
|
|
|
|
// L'URL des deux liens
|
|
$url = 'https://myx.ovh/nova/resources/accounts/'.$user->id;
|
|
|
|
$keyboard = [
|
|
'inline_keyboard' => [
|
|
[
|
|
['text' => 'Cliquez ici pour plus d\'infos', 'url' => $url]
|
|
]
|
|
]
|
|
];
|
|
|
|
// Convertir le tableau de clavier en JSON
|
|
$keyboardJson = json_encode($keyboard);
|
|
|
|
// Envoyer le message avec les deux boutons
|
|
Http::get('https://api.telegram.org/bot6784810105:AAEq3emnkRwdyvCLC-iqdIjVJ2Ke6HwwGjg/sendMessage', [
|
|
'chat_id' => '1970698501', // Remplacez par votre chat_id
|
|
'text' => $text,
|
|
'reply_markup' => $keyboardJson
|
|
]);
|
|
}
|
|
|
|
}
|
|
|
|
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 hashtags
|
|
$phrasePlusFrequente = preg_replace('/#\w+\s?/', '', $phrasePlusFrequente);
|
|
|
|
// Supprimer les tags
|
|
$phrasePlusFrequente = preg_replace('/@\w+\s?/', '', $phrasePlusFrequente);
|
|
|
|
// 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), '/')));
|
|
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)) {
|
|
return '@chauchettes';
|
|
}
|
|
|
|
//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)) {
|
|
return '@chauchettes @totorunior';
|
|
}
|
|
|
|
//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)) {
|
|
return '@chauchettes @totorunior @crakotte84';
|
|
}
|
|
}
|
|
}
|
|
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;
|
|
}
|
|
} |