Created
February 4, 2023 14:33
-
-
Save denrad/1572e7ff8837402634502bf33b924de1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Ищем в выгрузке твитов от Twitter запрещенные в РФ ссылки и показываем твиты, их содержащие | |
| */ | |
| ini_set('memory_limit', '1G'); // Для разбора больших JSON файлов нужен большой лимит | |
| const PROHIBITED_URLS = [ | |
| 'meduza.io', | |
| ]; | |
| function filterTweets(array $tweet): bool | |
| { | |
| foreach ($tweet['tweet']['entities']['urls'] ?? [] as $url) { | |
| if (in_array( | |
| parse_url($url['expanded_url'] ?? '', PHP_URL_HOST), | |
| PROHIBITED_URLS, | |
| true | |
| )) { | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| function getAccountUserName(): ?string | |
| { | |
| $accountContent = file_get_contents("data/account.js"); | |
| [, $json] = explode('=', $accountContent); | |
| $accountInfo = json_decode($json, true, 512, JSON_THROW_ON_ERROR); | |
| return $accountInfo[0]['account']['username'] ?? null; | |
| } | |
| function getJsonFromJs(string $filename): string | |
| { | |
| $content = file_get_contents($filename); | |
| [, $json] = explode('=', $content, 2); | |
| return trim($json); | |
| } | |
| $accountName = getAccountUserName(); | |
| $prohibitedTweets = $prohibitedTweetURls = []; | |
| $tweetFiles = glob("data/tweets*.js", GLOB_BRACE); | |
| echo "Поиск запрещенных ссылок в твитах от $accountName", PHP_EOL; | |
| echo "Всего файлов с твитами: ", count($tweetFiles), PHP_EOL; | |
| foreach ($tweetFiles as $tweetFile) { | |
| echo "Обработка файла $tweetFile", PHP_EOL; | |
| $json = getJsonFromJs($tweetFile); | |
| $tweets = json_decode($json, true, 512, JSON_THROW_ON_ERROR); | |
| echo "Найдено твитов: ", count($tweets), PHP_EOL; | |
| $prohibitedTweets[] = $tweets = array_filter($tweets, 'filterTweets'); | |
| echo "Найдено запрещенных твитов: ", count($tweets), PHP_EOL; | |
| } | |
| $prohibitedTweets = array_merge(...$prohibitedTweets); | |
| echo "Всего запрещенных твитов: ", count($prohibitedTweets), PHP_EOL; | |
| $getTweetUrl = function (array $tweet) use ($accountName): string { | |
| $id = $tweet['tweet']['id_str']; | |
| return "https://twitter.com/$accountName/status/{$id}"; | |
| }; | |
| echo "Ссылки на запрещенные твиты:", PHP_EOL; | |
| echo implode(PHP_EOL, array_map($getTweetUrl, $prohibitedTweets)); | |
| echo PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment