Skip to content

Instantly share code, notes, and snippets.

@loperd
Last active April 20, 2024 21:19
Show Gist options
  • Save loperd/b750d74da8f52623c14f0f4c70a70cc6 to your computer and use it in GitHub Desktop.
Save loperd/b750d74da8f52623c14f0f4c70a70cc6 to your computer and use it in GitHub Desktop.
Reddit invite code scrapper + php validator for mocaverse.xyz
{
"require": {
"guzzle/guzzle": "~3.9",
"ext-json": "*",
"ext-curl": "*",
"guzzlehttp/guzzle": "^7.8",
"ext-pcntl": "*"
}
}
const results = [];
for (let item of document.getElementsByTagName('shreddit-comment')) {
for (let comment of item.querySelectorAll('[slot=comment]')) {
for (let paragraph of comment.getElementsByTagName('p')) {
for (let match of paragraph.innerHTML.matchAll(/[A-Z0-9]{10}/g)) results[match[0]] = match[0];
}
}
}
console.log(Object.values(results).join('\n'));
<?php
declare(strict_types=1);
declare(ticks=1);
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
require_once __DIR__ . '/vendor/autoload.php';
$client = new Client([
'base_uri' => 'https://api.moca-id.mocaverse.xyz',
'timeout' => 2.0,
]);
$availableCodes = [];
$failedCodes = [];
if (file_exists('failed.txt')) {
$failedCodes = explode(PHP_EOL, file_get_contents('failed.txt'));
$failedCodes = array_filter($failedCodes, static fn (string $code) => '' !== $code);
}
if (file_exists('available.txt')) {
$availableCodes = explode(PHP_EOL, file_get_contents('available.txt'));
$availableCodes = array_filter($availableCodes, static fn (string $code) => '' !== $code);
}
$skipCodes = array_unique(array_merge($availableCodes, $failedCodes));
$codes = explode(PHP_EOL, file_get_contents('codes.txt'));
$codes = array_map(static fn (string $code) => trim($code), $codes);
$codes = array_filter($codes, static function (string $code) use ($skipCodes) {
return !in_array($code, $skipCodes, true) && $code !== '';
});
$codes = array_values($codes);
function saveCodes(): void
{
global $failedCodes, $exit, $availableCodes;
echo 'Save failed codes...' . PHP_EOL;
file_put_contents('failed.txt', implode(PHP_EOL, $failedCodes));
file_put_contents('available.txt', implode(PHP_EOL, $availableCodes));
$exit = true;
}
$exit = false;
register_tick_function(static function () {
global $exit;
if ($exit) {
exit;
}
});
pcntl_signal(SIGTERM, 'saveCodes');
pcntl_signal(SIGINT, 'saveCodes');
pcntl_signal(SIGHUP, 'saveCodes');
pcntl_signal(SIGUSR1, 'saveCodes');
try {
$retries = 1;
for ($i = 0, $max = count($codes); $i < $max; ) {
$code = $codes[$i];
if (in_array($code, $failedCodes, true)) {
$i++;
continue;
}
$templateUri = '/api/referral-codes/verify';
try {
printf('Trying code: %s', $code.PHP_EOL);
$headers = [];
$headers['authority'] = 'api.moca-id.mocaverse.xyz';
$headers['accept'] = 'application/json, text/plain, */*';
$headers['accept-language'] = 'en-US,en;q=0.9';
$headers['content-type'] = 'application/json';
$headers['origin'] = 'https://www.mocaverse.xyz';
$headers['referer'] = 'https://www.mocaverse.xyz/';
$headers['sec-ch-ua'] = '"Not A(Brand";v="99", "Google Chrome";v="121", "Chromium";v="121"';
$headers['sec-ch-ua-platform'] = '"macOS"';
$headers['user-agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36';
$body = json_encode(['referralCode' => $code], JSON_THROW_ON_ERROR);
$response = $client->send(new Request('POST', sprintf($templateUri, $code), $headers, $body));
$response = json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
printf('Code accessed: %s'.PHP_EOL, $code);
$availableCodes[] = $code;
usleep(1500000);
$i++;
} catch (RequestException $ex) {
$failedResponse = $ex->getResponse();
if ($failedResponse->getStatusCode() === 429) {
echo "Retry, because too many requests" . PHP_EOL;
$retries++;
usleep(3500000 * $retries);
continue;
}
$retries = 1;
$i++;
try {
$contents = json_decode($failedResponse->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
$message = $contents['message'];
} catch (\Throwable) {
$message = 'Unknown error';
}
printf('Code: "%s" is failed [%s]'.PHP_EOL, $code, $message);
$failedCodes[] = $code;
usleep(1500000);
continue;
}
}
echo 'I\'m done'.PHP_EOL;
} finally {
saveCodes();
}
@loperd
Copy link
Author

loperd commented Apr 20, 2024

Paste the js script into the developer console on the reddit page https://www.reddit.com/r/Animoca/comments/1ag6tt2/share_your_mocaverse_invite_codes_here/.
And you will get all the codes, put them in the codes.txt file and run the php script to get all the available codes. Each actual code will be written to the {code}.txt file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment