Skip to content

Commit

Permalink
Update legacy to php 8
Browse files Browse the repository at this point in the history
Added ac.nowcoder.com
Problem rating predictor
Added archive problems
Codechef ratelimit fix
  • Loading branch information
aropan committed Aug 11, 2024
1 parent b93933f commit 86fbc2d
Show file tree
Hide file tree
Showing 76 changed files with 1,472 additions and 393 deletions.
17 changes: 15 additions & 2 deletions legacy/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM php:7-fpm
FROM php:8-fpm

# Use the default production configuration
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
Expand All @@ -8,13 +8,26 @@ RUN docker-php-ext-install iconv
RUN apt install -y libicu-dev && docker-php-ext-install intl
RUN apt install -y libpq-dev && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql && docker-php-ext-install pgsql

# Curl
ENV CURL_VERSION=8.9.1
RUN curl -L https://github.com/stunnel/static-curl/releases/download/${CURL_VERSION}/curl-linux-aarch64-${CURL_VERSION}.tar.xz --output /tmp/curl.tar.xz && \
tar -xvf /tmp/curl.tar.xz -C /tmp && \
sha256sum /tmp/curl | grep -q "eff254056270ded081d677f2f04847817a1be692283bd5b37fbc379bed8d5461" && \
mv /tmp/curl /usr/local/bin/curl && \
rm /tmp/curl.tar.xz

# Configure python
COPY requirements.txt .
RUN apt install -y python3 python3-pip && pip install -r requirements.txt
RUN apt install -y python3 python3-pip python3-venv && \
python3 -m venv /venv/legacy && \
/venv/legacy/bin/pip install -U pip && \
/venv/legacy/bin/pip install -r requirements.txt
ENV PATH="/venv/legacy/bin/:$PATH"

# Configure cron
RUN apt install -y cron
COPY cron /etc/cron.d/clist
RUN echo "PATH=$PATH" | cat - /etc/cron.d/clist > temp && mv temp /etc/cron.d/clist
RUN chmod 0644 /etc/cron.d/clist
RUN crontab /etc/cron.d/clist

Expand Down
1 change: 0 additions & 1 deletion legacy/cron
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

7,25,42 * * * * /usr/src/legacy/update.bash >/usr/src/legacy/logs/cron.log 2>&1
8 changes: 8 additions & 0 deletions legacy/db.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
$is_debug = true;
class db
{
private string $host;
private string $dbname;
private int $port;
private string $username;
private string $password;
private $link;
private $result;

function __construct()
{
$db_conf = parse_ini_file("/run/secrets/db_conf");
Expand Down
39 changes: 35 additions & 4 deletions legacy/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,11 @@ function curlexec(&$url, $postfields = NULL, $params = array())
echo "url = $url\n";
}

// echo "curl_setopt(CURLOPT_URL, $url)\n";
empty($url) && die("Empty URL on " . __FILE__ . ":" . __LINE__);
curl_setopt($CID, CURLOPT_URL, $url);

$header = array('Accept-Language: en;q=0.8, ru;q=0.2');
$header = array();
$header[] = 'Accept-Language: en;q=0.8, ru;q=0.2';
if (isset($params["http_header"])) {
$header = array_merge($header, $params["http_header"]);
}
Expand All @@ -193,13 +193,30 @@ function curlexec(&$url, $postfields = NULL, $params = array())
curl_setopt($CID, CURLOPT_POST, false);
}

$with_curl = isset($params["with_curl"]) && $params["with_curl"];

if (CACHE && $postfields === NULL && file_exists($cachefile))
{
$page = file_get_contents($cachefile);
}
else
{
$page = curl_exec($CID);
if ($with_curl) {
$command = "curl -i {$url} -L --silent";
foreach ($header as $h) {
$command .= " -H " . escapeshellarg($h);
}
if ($postfields !== NULL) {
$postfields = http_build_query($postfields);
$command .= " --data " . escapeshellarg($postfields);
}
if (isset($params["cookie_file"])) {
$command .= " -b " . escapeshellarg($params["cookie_file"]) . " -c " . escapeshellarg($params["cookie_file"]);
}
$page = shell_exec($command);
} else {
$page = curl_exec($CID);
}
if (preg_match('#charset=["\']?([-a-z0-9]+)#i', $page, $match))
{
$charset = $match[1];
Expand Down Expand Up @@ -254,7 +271,16 @@ function curlexec(&$url, $postfields = NULL, $params = array())
$COOKIE[$k] = $v;
}
}
$url = curl_getinfo($CID, CURLINFO_EFFECTIVE_URL);
if ($with_curl) {
if (isset($header['location'])) {
$url = $header['location'];
if (is_array($url)) {
$url = end($url);
}
}
} else {
$url = curl_getinfo($CID, CURLINFO_EFFECTIVE_URL);
}
return $page;
}

Expand Down Expand Up @@ -447,6 +473,11 @@ function unparse_url($parsed_url)
return "$scheme$user$pass$host$port$path$query$fragment";
}


function parse_schema_host($url) {
return parse_url($url, PHP_URL_SCHEME) . "://" . parse_url($url, PHP_URL_HOST);
}

function url_merge($original, $new, $merge_query = false)
{
if (is_string($original)) {
Expand Down
2 changes: 1 addition & 1 deletion legacy/libs/smarty
Submodule smarty updated 1243 files
81 changes: 81 additions & 0 deletions legacy/module/ac.nowcoder.com/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
require_once dirname(__FILE__) . "/../../config.php";

$seen = array();

function process_url($url) {
global $contests, $RID, $HOST, $TIMEZONE;
global $seen;

$page = curlexec($url);
preg_match_all('#<div[^>]*data-id="(?P<id>[^"]*)"[^<]*data-json="(?P<data>[^"]*)"[^>]*>#', $page, $matches, PREG_SET_ORDER);
$n_parsed = 0;

foreach ($matches as $_ => $match) {
$data = html_entity_decode($match['data']);
$data = html_entity_decode($data);
$data = json_decode($data, true);

$type = pop_item($data, 'type');
if ($type == 0) {
$kind = 'ACM';
$standings_kind = 'icpc';
} elseif ($type == 3) {
$kind = 'IOI';
$standings_kind = 'scoring';
} elseif ($type == 2) {
$kind = 'OI';
$standings_kind = 'scoring';
} else {
$kind = null;
$standings_kind = null;
}

$title = html_entity_decode(pop_item($data, 'contestName'));
if ($kind) {
$title .= " [$kind]";
}

$key = pop_item($data, 'contestId');

$contest = [
'title' => $title,
'start_time' => pop_item($data, 'contestStartTime') / 1000,
'end_time' => pop_item($data, 'contestEndTime') / 1000,
'duration' => pop_item($data, 'contestDuration') / 1000 / 60,
'url' => "https://ac.nowcoder.com/acm/contest/{$key}",
'key' => $key,
'rid' => $RID,
'host' => $HOST,
'timezone' => $TIMEZONE,
'info' => ['parse' => $data],
];

if ($standings_kind) {
$contest['standings_kind'] = $standings_kind;
}

$contests[] = $contest;

if (!isset($seen[$key])) {
$seen[$key] = true;
$n_parsed += 1;
}
}
return $n_parsed;
}

process_url($URL);

$page = 1;
$base_url = parse_schema_host($URL) . '/acm/contest/vip-end-index?orderType=DESC';

for ($page = 1; ; $page++) {
$url = $base_url . "&page=$page";
$ok = process_url($url);

if (!$ok || !isset($_GET['parse_full_list'])) {
break;
}
}
?>
2 changes: 1 addition & 1 deletion legacy/module/acmp.ru/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}

if (DEBUG) {
echo "$year - ${match['title']}\n";
echo "$year - {$match['title']}\n";
}

preg_match_all('#<a[^>]*href="?(?P<href>[^ ">]*)"?>\[Описание\]</a>#', $page, $matches, PREG_SET_ORDER);
Expand Down
2 changes: 1 addition & 1 deletion legacy/module/adventofcode.com/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
preg_match_all('#<a[^>]*aria-label="(?P<label>[^"]*)"[^>]*href="(?P<href>[^>]*/day/[^>]*)"[^>]*class="calendar-day(?P<day>[0-9]+)"[^>]*>#', $page, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$day = $match['day'];
$title = "$name. ${match['label']}";
$title = "$name. {$match['label']}";
$contests[] = array(
'start_time' => "$day.12.$year 00:00",
'duration' => '00:00',
Expand Down
18 changes: 14 additions & 4 deletions legacy/module/beecrowd.com.br/index.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
<?php
require_once dirname(__FILE__) . '/../../config.php';
$url_scheme_host = parse_url($URL, PHP_URL_SCHEME) . "://" . parse_url($URL, PHP_URL_HOST);

$user_agent_file = dirname(__FILE__) . "/../../logs/beecrowd.user_agent";
$cookie_file = dirname(__FILE__) . "/../../logs/beecrowd.cookie";
$header = array();
if (file_exists($user_agent_file)) {
$user_agent = trim(file_get_contents($user_agent_file));
$header[] = "User-Agent: $user_agent";
}
$curlexec_params = ['http_header' => $header, 'with_curl' => true, 'cookie_file' => $cookie_file];

for ($n_page = 1;; $n_page += 1) {
$url = "https://www.beecrowd.com.br/judge/en/contests?page=$n_page";
$page = curlexec($url);
$url = $url_scheme_host . "/en/contests?page=$n_page";
$page = curlexec($url, null, $curlexec_params);

if (strpos($url, '/login') !== false) {
preg_match_all('#<input[^>]*name="(?P<name>[^"]*)"(?:[^>]*value="(?P<value>[^"]*)")?[^>]*>#', $page, $matches, PREG_SET_ORDER);
Expand All @@ -14,7 +24,7 @@
require_once dirname(__FILE__) . '/secret.php';
$fields['email'] = $BEECROWD_EMAIL;
$fields['password'] = $BEECROWD_PASSWORD;
$page = curlexec($url, $fields);
$page = curlexec($url, $fields, $curlexec_params);
}

preg_match_all('#
Expand All @@ -36,7 +46,7 @@
$url = url_merge($URL, $c['url']);
$title = $c['title'];
if (substr($title, -3) == '...') {
$page = curlexec($url);
$page = curlexec($url, null, $curlexec_params);
if (preg_match('#</h2>\s*<p[^>]*>(?P<title>[^<]*)</p>#', $page, $match)) {
$title = $match['title'];
}
Expand Down
4 changes: 2 additions & 2 deletions legacy/module/bubblecup.org/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
continue;
}

$year = strftime('%Y', strtotime($match['start_time']));
$month = intval(strftime('%m', strtotime($match['start_time'])));
$year = date('Y', strtotime($match['start_time']));
$month = intval(date('m', strtotime($match['start_time'])));
if ($month >= 9) {
$year = $year + 1;
}
Expand Down
2 changes: 1 addition & 1 deletion legacy/module/challenges.reply.com/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
trigger_error("Invalid singin: $data", E_USER_WARNING);
return;
} else if (!isset($data["message"]) || strpos(strtolower($data["message"]), "success") === false) {
trigger_error("Invalid singin: ${data['message']}", E_USER_WARNING);
trigger_error("Invalid singin: {$data['message']}", E_USER_WARNING);
return;
}
$url = 'https://challenges.reply.com/tamtamy/api/user-in-session.json';
Expand Down
2 changes: 1 addition & 1 deletion legacy/module/codeforces.com-gym/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
}
if ($json['status'] != 'OK') {
$json_str = print_r($json, true);
trigger_error("status = ${json['status']}, json = $json_str", E_USER_WARNING);
trigger_error("status = {$json['status']}, json = $json_str", E_USER_WARNING);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion legacy/module/codeforces.com/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
if (!is_array($json)) {
return;
}
$json['status'] == 'OK' or trigger_error("status = '${json['status']}' for $url");
$json['status'] == 'OK' or trigger_error("status = '{$json['status']}' for $url");
$global_rounds_stage_year = false;
foreach ($json['result'] as $c) {
$title = $c['name'];
Expand Down
4 changes: 2 additions & 2 deletions legacy/module/coderun.yandex.ru/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
$url = "/api/seasons/$slug/season-info";
$data = curlexec($url, null, ['json_output' => true]);
if (isset($data['error'])) {
trigger_error("Warning error on $slug season: ${data['error']}", E_USER_WARNING);
trigger_error("Warning error on $slug season: {$data['error']}", E_USER_WARNING);
continue;
}
$data = get_item($data, ['result']);
Expand All @@ -29,7 +29,7 @@
$data['slug'] = $slug;
$data['track'] = $track;
$title = $data['title'] . '. ' . $track['title'];
$url = url_merge($HOST_URL, "/seasons/$slug/tracks/${track['slug']}/");
$url = url_merge($HOST_URL, "/seasons/$slug/tracks/{$track['slug']}/");
$start_time = $data['startDate'];
$end_time = $data['endDate'];
$key = $slug . '/' . $track['slug'];
Expand Down
2 changes: 1 addition & 1 deletion legacy/module/codingninjas.com_codestudio/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
'start_time' => $start_time,
'end_time' => $end_time,
'title' => $c['name'],
'url' => "$URL/${c['slug']}",
'url' => "$URL/{$c['slug']}",
'host' => $HOST,
'rid' => $RID,
'timezone' => $TIMEZONE,
Expand Down
2 changes: 1 addition & 1 deletion legacy/module/ctftime.org/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
foreach ($data["items"] as $item) {
if (!preg_match("#https?[^\s]*$#", $item["description"], $matches)) {
trigger_error("No found url in ${item["description"]}", E_USER_WARNING);
trigger_error("No found url in {$item["description"]}", E_USER_WARNING);
}
$url = $matches[0];

Expand Down
4 changes: 2 additions & 2 deletions legacy/module/dphi.tech/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}

foreach ($response['results'] as $c) {
$cid = "${c['challenge_id']}";
$cid = "{$c['challenge_id']}";

$title = $c['title'];
if ($c['datathon_type']) {
Expand All @@ -26,7 +26,7 @@
'start_time' => $c['start_date'],
'end_time' => $c['end_date'],
'title' => $title,
'url' => url_merge($URL, "/challenges/${c['slug']}/$cid/"),
'url' => url_merge($URL, "/challenges/{$c['slug']}/$cid/"),
'host' => $HOST,
'rid' => $RID,
'timezone' => $TIMEZONE,
Expand Down
2 changes: 1 addition & 1 deletion legacy/module/facebook.com/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ function get_ids($page) {
foreach ($season['season_contests']['nodes'] as $node) {
$info = array('parse' => $node);
$year = $node['contest_season']['season_vanity'];
$url = rtrim($URL, '/') . "/$year/${node['contest_vanity']}";
$url = rtrim($URL, '/') . "/$year/{$node['contest_vanity']}";
$scoreboard_url = rtrim($url) . '/scoreboard';
$url_ = $scoreboard_url;
$scoreboard_page = curlexec($url_);
Expand Down
2 changes: 1 addition & 1 deletion legacy/module/geeksforgeeks.org/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
'start_time' => $c['start_time'],
'end_time' => $c['end_time'],
'title' => $title,
'url' => url_merge($URL, "/contest/${c["slug"]}/"),
'url' => url_merge($URL, "/contest/{$c["slug"]}/"),
'host' => $HOST,
'rid' => $RID,
'timezone' => $TIMEZONE,
Expand Down
Loading

0 comments on commit 86fbc2d

Please sign in to comment.