Delete tools/gdown.py
Browse files- tools/gdown.py +0 -100
tools/gdown.py
DELETED
|
@@ -1,100 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import re
|
| 3 |
-
import sys
|
| 4 |
-
import json
|
| 5 |
-
import codecs
|
| 6 |
-
import tempfile
|
| 7 |
-
import requests
|
| 8 |
-
|
| 9 |
-
from urllib.parse import urlparse, parse_qs, unquote
|
| 10 |
-
|
| 11 |
-
def parse_url(url):
|
| 12 |
-
parsed = urlparse(url)
|
| 13 |
-
is_download_link = parsed.path.endswith("/uc")
|
| 14 |
-
if not parsed.hostname in ("drive.google.com", "docs.google.com"): return None, is_download_link
|
| 15 |
-
file_id = parse_qs(parsed.query).get("id", [None])[0]
|
| 16 |
-
|
| 17 |
-
if file_id is None:
|
| 18 |
-
for pattern in (r"^/file/d/(.*?)/(edit|view)$", r"^/file/u/[0-9]+/d/(.*?)/(edit|view)$", r"^/document/d/(.*?)/(edit|htmlview|view)$", r"^/document/u/[0-9]+/d/(.*?)/(edit|htmlview|view)$", r"^/presentation/d/(.*?)/(edit|htmlview|view)$", r"^/presentation/u/[0-9]+/d/(.*?)/(edit|htmlview|view)$", r"^/spreadsheets/d/(.*?)/(edit|htmlview|view)$", r"^/spreadsheets/u/[0-9]+/d/(.*?)/(edit|htmlview|view)$"):
|
| 19 |
-
match = re.match(pattern, parsed.path)
|
| 20 |
-
if match:
|
| 21 |
-
file_id = match.group(1)
|
| 22 |
-
break
|
| 23 |
-
return file_id, is_download_link
|
| 24 |
-
|
| 25 |
-
def get_url_from_gdrive_confirmation(contents):
|
| 26 |
-
for pattern in (r'href="(\/uc\?export=download[^"]+)', r'href="/open\?id=([^"]+)"', r'"downloadUrl":"([^"]+)'):
|
| 27 |
-
match = re.search(pattern, contents)
|
| 28 |
-
if match:
|
| 29 |
-
url = match.group(1)
|
| 30 |
-
if pattern == r'href="/open\?id=([^"]+)"': url = (codecs.decode("uggcf://qevir.hfrepbagrag.tbbtyr.pbz/qbjaybnq?vq=", "rot13") + url + "&confirm=t&uuid=" + re.search(r'<input\s+type="hidden"\s+name="uuid"\s+value="([^"]+)"', contents).group(1))
|
| 31 |
-
elif pattern == r'"downloadUrl":"([^"]+)': url = url.replace("\\u003d", "=").replace("\\u0026", "&")
|
| 32 |
-
else: url = codecs.decode("uggcf://qbpf.tbbtyr.pbz", "rot13") + url.replace("&", "&")
|
| 33 |
-
return url
|
| 34 |
-
|
| 35 |
-
match = re.search(r'<p class="uc-error-subcaption">(.*)</p>', contents)
|
| 36 |
-
if match: raise Exception(match.group(1))
|
| 37 |
-
raise Exception
|
| 38 |
-
|
| 39 |
-
def _get_session(use_cookies, return_cookies_file=False):
|
| 40 |
-
sess = requests.session()
|
| 41 |
-
sess.headers.update({"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)"})
|
| 42 |
-
cookies_file = os.path.join(os.path.expanduser("~"), ".cache/gdown/cookies.json")
|
| 43 |
-
|
| 44 |
-
if os.path.exists(cookies_file) and use_cookies:
|
| 45 |
-
with open(cookies_file) as f:
|
| 46 |
-
for k, v in json.load(f):
|
| 47 |
-
sess.cookies[k] = v
|
| 48 |
-
return (sess, cookies_file) if return_cookies_file else sess
|
| 49 |
-
|
| 50 |
-
def gdown_download(url=None, id=None, output=None):
|
| 51 |
-
if not (id is None) ^ (url is None): raise ValueError
|
| 52 |
-
if id is not None: url = f"{codecs.decode('uggcf://qevir.tbbtyr.pbz/hp?vq=', 'rot13')}{id}"
|
| 53 |
-
|
| 54 |
-
url_origin = url
|
| 55 |
-
sess, cookies_file = _get_session(use_cookies=True, return_cookies_file=True)
|
| 56 |
-
gdrive_file_id, is_gdrive_download_link = parse_url(url)
|
| 57 |
-
|
| 58 |
-
if gdrive_file_id:
|
| 59 |
-
url = f"{codecs.decode('uggcf://qevir.tbbtyr.pbz/hp?vq=', 'rot13')}{gdrive_file_id}"
|
| 60 |
-
url_origin = url
|
| 61 |
-
is_gdrive_download_link = True
|
| 62 |
-
|
| 63 |
-
while 1:
|
| 64 |
-
res = sess.get(url, stream=True, verify=True)
|
| 65 |
-
if url == url_origin and res.status_code == 500:
|
| 66 |
-
url = f"{codecs.decode('uggcf://qevir.tbbtyr.pbz/bcra?vq=', 'rot13')}{gdrive_file_id}"
|
| 67 |
-
continue
|
| 68 |
-
|
| 69 |
-
os.makedirs(os.path.dirname(cookies_file), exist_ok=True)
|
| 70 |
-
with open(cookies_file, "w") as f:
|
| 71 |
-
json.dump([(k, v) for k, v in sess.cookies.items() if not k.startswith("download_warning_")], f, indent=2)
|
| 72 |
-
|
| 73 |
-
if "Content-Disposition" in res.headers: break
|
| 74 |
-
if not (gdrive_file_id and is_gdrive_download_link): break
|
| 75 |
-
|
| 76 |
-
try:
|
| 77 |
-
url = get_url_from_gdrive_confirmation(res.text)
|
| 78 |
-
except Exception as e:
|
| 79 |
-
raise Exception(e)
|
| 80 |
-
|
| 81 |
-
if gdrive_file_id and is_gdrive_download_link:
|
| 82 |
-
content_disposition = unquote(res.headers["Content-Disposition"])
|
| 83 |
-
filename_from_url = (re.search(r"filename\*=UTF-8''(.*)", content_disposition) or re.search(r'filename=["\']?(.*?)["\']?$', content_disposition)).group(1).replace(os.path.sep, "_")
|
| 84 |
-
else: filename_from_url = os.path.basename(url)
|
| 85 |
-
|
| 86 |
-
output = os.path.join(output or ".", filename_from_url)
|
| 87 |
-
tmp_file = tempfile.mktemp(suffix=tempfile.template, prefix=os.path.basename(output), dir=os.path.dirname(output))
|
| 88 |
-
f = open(tmp_file, "ab")
|
| 89 |
-
|
| 90 |
-
if tmp_file is not None and f.tell() != 0: res = sess.get(url, headers={"Range": f"bytes={f.tell()}-"}, stream=True, verify=True)
|
| 91 |
-
print("To:", os.path.abspath(output), file=sys.stderr)
|
| 92 |
-
|
| 93 |
-
try:
|
| 94 |
-
for chunk in res.iter_content(chunk_size=512 * 1024):
|
| 95 |
-
f.write(chunk)
|
| 96 |
-
if tmp_file: f.close()
|
| 97 |
-
finally:
|
| 98 |
-
os.rename(tmp_file, output)
|
| 99 |
-
sess.close()
|
| 100 |
-
return output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|