From 144b02e54c98718ba70b291bba2fec31ecba2fee Mon Sep 17 00:00:00 2001 From: zsgsdesign Date: Sun, 24 Oct 2021 14:56:16 +0800 Subject: [PATCH] always fetch English attachment first --- Crawler.php | 19 +++++++++++++++++-- babel.json | 2 +- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Crawler.php b/Crawler.php index d843a7d..cd3b79f 100644 --- a/Crawler.php +++ b/Crawler.php @@ -104,8 +104,23 @@ private function _extractCodeForces($pcode, $url) if (stripos($content, "Attachments") !== false) { // refetching actual attachment - $attachmentDOM = HtmlDomParser::str_get_html($content, true, true, DEFAULT_TARGET_CHARSET, false); - $attachmentURL = $attachmentDOM->find('#pageContent div.datatable tbody tr a', 0)->href; + $attachmentURL = null; + $attachmentsDOM = HtmlDomParser::str_get_html($content, true, true, DEFAULT_TARGET_CHARSET, false); + foreach ($attachmentsDOM->find('#pageContent div.datatable tbody tr') as $attachmentDOM) { + if(filled($attachmentDOM->find('a', 0)) && trim($attachmentDOM->find('td', 1)->plaintext == 'English')) { + $attachmentURL = $attachmentDOM->find('a', 0)->href; + break; + } + } + if(blank($attachmentURL)) { + $firstAttachmentDOM = $attachmentsDOM->find('#pageContent div.datatable tbody tr a', 0); + if(filled($firstAttachmentDOM)) { + $attachmentURL = $firstAttachmentDOM->href; + } else { + $this->line("\n <bg=red;fg=white> Exception </> : <fg=yellow>Statement is not available.</>\n"); + return false; + } + } $url = $this->globalizeCodeForcesURL($attachmentURL); $response = $this->getCodeForcesResponse($url); $contentType = $response->headers['content-type']; diff --git a/babel.json b/babel.json index 2d2c958..2080169 100644 --- a/babel.json +++ b/babel.json @@ -6,7 +6,7 @@ "description": "CodeForces Gym Interface for NOJ", "license": "MIT", "repository": "https://github.com/NJUPTAAA/NOJ_Extension_Babel_Gym", - "version": "0.1.3", + "version": "0.1.4", "website": "https://codeforces.com/gyms", "custom": { "css": "resources/custom.css"