| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
"""Extractors for https://gofile.io/""" |
| 8 |
|
| 9 |
from .common import Extractor, Message |
| 10 |
import hashlib |
| 11 |
import time |
| 12 |
|
| 13 |
|
| 14 |
class GofileFolderExtractor(Extractor): |
| 15 |
category = "gofile" |
| 16 |
subcategory = "folder" |
| 17 |
root = "https://gofile.io" |
| 18 |
directory_fmt = ("{category}", "{name} ({code})") |
| 19 |
archive_fmt = "{id}" |
| 20 |
pattern = r"(?:https?://)?(?:www\.)?gofile\.io/d/([^/?#]+)" |
| 21 |
example = "https://gofile.io/d/ID" |
| 22 |
|
| 23 |
def items(self): |
| 24 |
recursive = self.config("recursive", True) |
| 25 |
password = self.config("password") |
| 26 |
|
| 27 |
token = self.config("api-token") |
| 28 |
if not token: |
| 29 |
token = self.cache(self._create_account, _key=None) |
| 30 |
self.cookies.set("accountToken", token, domain=".gofile.io") |
| 31 |
self.api_token = token |
| 32 |
|
| 33 |
folder = self._get_content(self.groups[0], password) |
| 34 |
yield Message.Directory, "", folder |
| 35 |
|
| 36 |
if not folder.get("canAccess"): |
| 37 |
raise self.exc.AuthorizationError("Password required") |
| 38 |
|
| 39 |
contents = (folder.pop("children").values() |
| 40 |
if "children" in folder else (folder,)) |
| 41 |
|
| 42 |
num = 0 |
| 43 |
for content in contents: |
| 44 |
content["folder"] = folder |
| 45 |
|
| 46 |
if content["type"] == "file": |
| 47 |
num += 1 |
| 48 |
content["num"] = num |
| 49 |
content["filename"], _, content["extension"] = \ |
| 50 |
content["name"].rpartition(".") |
| 51 |
yield Message.Url, content["link"], content |
| 52 |
|
| 53 |
elif content["type"] == "folder": |
| 54 |
if recursive: |
| 55 |
url = "https://gofile.io/d/" + content["id"] |
| 56 |
content["_extractor"] = GofileFolderExtractor |
| 57 |
yield Message.Queue, url, content |
| 58 |
else: |
| 59 |
self.log.debug("Skipping subfolder '%s'", content["id"]) |
| 60 |
else: |
| 61 |
self.log.debug("'%s' is of unknown type (%s)", |
| 62 |
content.get("name"), content["type"]) |
| 63 |
|
| 64 |
def request_api(self, endpoint, params=None, headers=None, method="GET"): |
| 65 |
response = self.request_json( |
| 66 |
"https://api.gofile.io" + endpoint, |
| 67 |
method=method, params=params, headers=headers) |
| 68 |
|
| 69 |
if response["status"] != "ok": |
| 70 |
if response["status"] == "error-notFound": |
| 71 |
raise self.exc.NotFoundError("content") |
| 72 |
if response["status"] == "error-passwordRequired": |
| 73 |
raise self.exc.AuthorizationError("Password required") |
| 74 |
raise self.exc.AbortExtraction( |
| 75 |
f"{endpoint} failed (Status: {response['status']})") |
| 76 |
|
| 77 |
return response["data"] |
| 78 |
|
| 79 |
def _create_account(self): |
| 80 |
self.log.debug("Creating temporary account") |
| 81 |
return self.request_api("/accounts", method="POST")["token"] |
| 82 |
|
| 83 |
def _generate_website_token(self, lang="en-US"): |
| 84 |
|
| 85 |
data = (f"{self.session.headers['User-Agent']}::" |
| 86 |
f"{lang}::" |
| 87 |
f"{self.api_token}::" |
| 88 |
f"{int(time.time() / 14400)}::" |
| 89 |
f"{self.config('salt') or '12af056dacea0b'}") |
| 90 |
return hashlib.sha256(data.encode()).hexdigest() |
| 91 |
|
| 92 |
def _get_content(self, content_id, password=None): |
| 93 |
params = { |
| 94 |
"contentFilter": "" , |
| 95 |
"page" : "1", |
| 96 |
"pageSize" : "1000", |
| 97 |
"sortField" : "name", |
| 98 |
"sortDirection": "1", |
| 99 |
"password" : None if password is None else |
| 100 |
hashlib.sha256(password.encode()).hexdigest(), |
| 101 |
} |
| 102 |
headers = { |
| 103 |
"Authorization" : "Bearer " + self.api_token, |
| 104 |
"X-Website-Token": self._generate_website_token("en-US"), |
| 105 |
"X-BL" : "en-US", |
| 106 |
} |
| 107 |
return self.request_api("/contents/" + content_id, params, headers) |