Download app.py from Luisito95/BitFlush: direct link, hf CLI and curl.
- Browser
- Download file 5.79 kB
-
https://huggingface.co/spaces/Luisito95/BitFlush/resolve/main/app.py
- Command line
-
hf download hf://spaces/Luisito95/BitFlush/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Luisito95/BitFlush/resolve/main/app.py
5.79 kB
| from flask import Flask, request, jsonify | |
| import threading | |
| import requests, subprocess | |
| import uuid | |
| import os, json | |
| from urllib.parse import urlparse | |
| from bs4 import BeautifulSoup | |
| def savedata(code, data): | |
| open("./codes/"+code, "w").write(json.dumps(data)) | |
| class BitFlush: | |
| def __init__(self): | |
| self.host = "https://cursos.ucf.edu.cu" | |
| self.username = "luisernestorb" | |
| self.password = "Luisito1995*" | |
| self.session = requests.Session() | |
| self.session.headers.update({"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36"}) | |
| self.sesskey = None | |
| def _login(self): | |
| _login_url = "https://cursos.ucf.edu.cu/login/index.php" | |
| _gettoken = self.session.get(_login_url) | |
| if "Actualmente ha iniciado sesión como" in _gettoken.text: | |
| return True | |
| logintoken = BeautifulSoup(_gettoken.text,"html.parser").find('input',{'name':'logintoken'})["value"] | |
| _signin = self.session.post(_login_url, data={"logintoken":logintoken, "username":self.username, "password":self.password}) | |
| if "Área personal" in _signin.text: | |
| self.sesskey = _signin.text.split('"sesskey":"')[1].split('"')[0] | |
| return True | |
| else: | |
| return False | |
| def _new_draftfile(self): | |
| _service = self.session.post(f"https://cursos.ucf.edu.cu/lib/ajax/service.php?sesskey={self.sesskey}&info=core_get_fragment", json=[{"index":0,"methodname":"core_get_fragment","args":{"component":"calendar","callback":"event_form","contextid":2,"args":[{"name":"courseid","value":1}]}}]).json()[0]["data"]["html"] | |
| self.userid = BeautifulSoup(_service,"html.parser").find('input',{'name':"userid"})["value"] | |
| self.context = int(_service.split("ctx_id=")[1].split("&")[0]) | |
| self.itemid = BeautifulSoup(_service,"html.parser").find('input',{'name':"description[itemid]"})["value"] | |
| _managefile = self.session.get(f"https://cursos.ucf.edu.cu/lib/editor/atto/plugins/managefiles/manage.php?elementid=id_description&context={self.context}&areamaxbytes=-1&maxbytes=0&subdirs=0&return_types=15&removeorphaneddrafts=0&itemid={self.itemid}") | |
| self.client_id = _managefile.text.split('"client_id":"')[1].split('"')[0] | |
| def _import_draftfile(self, token): | |
| token = token.split("-") | |
| self.client_id = token[0] | |
| self.itemid = token[1] | |
| def _upload_bytes(self, name, bytes): | |
| _updata = self.session.post("https://cursos.ucf.edu.cu/repository/repository_ajax.php?action=upload", data={"title":"", "author":"luisernestorb", "license":"unknown", "itemid":self.itemid, "repo_id":5, "p":"", "page":"", "env":"filemanager", "sesskey":self.sesskey, "client_id":self.client_id, "itemid":self.itemid, "maxbytes":104857600, "areamaxbytes":-1, "ctx_id":self.context, "savepath":"/"}, files={"repo_upload_file":(name, bytes)}) | |
| return _updata.json() | |
| def list(self): | |
| _listdata = self.session.post("https://cursos.ucf.edu.cu/repository/draftfiles_ajax.php?action=list", data={"sesskey":self.sesskey, "client_id":self.client_id, "filepath":"/", "itemid":self.itemid}) | |
| return _listdata.json() | |
| def delete(self, filename): | |
| _deldata = self.session.post("https://cursos.ucf.edu.cu/repository/draftfiles_ajax.php?action=delete", data={"sesskey":self.sesskey, "client_id":self.client_id, "filepath":"/", "itemid":self.itemid, "filename":filename}) | |
| return _deldata.json() | |
| def upload(self, path, code): | |
| self._login() | |
| self._new_draftfile() | |
| filename = path.split("/")[-1] | |
| current = 0 | |
| partsize = 20*1024*1024 | |
| total = (os.path.getsize(path)//partsize)+1 | |
| savedata(code, {"current":current, "total":total, "token":None}) | |
| with open(path, "rb") as file: | |
| while True: | |
| partname = filename+"."+str(current).zfill(3) | |
| partbytes = file.read(partsize) | |
| if not partbytes: | |
| break | |
| self._upload_bytes(partname, partbytes) | |
| current += 1 | |
| savedata(code, {"current":current, "total":total, "token":None}) | |
| savedata(code, {"current":current, "total":total, "token":f"{self.client_id}-{self.itemid}"}) | |
| os.unlink(path) | |
| return f"{self.client_id}-{self.itemid}" | |
| app = Flask(__name__) | |
| downloads = {} | |
| lock = threading.Lock() | |
| subprocess.Popen(["venv/bin/python","main.py"]) | |
| if not os.path.exists("./codes"): | |
| os.mkdir("./codes") | |
| def check_progress(): | |
| code = request.json.get('code') | |
| if not os.path.exists("./codes/"+code): | |
| return jsonify({"error": "Código inválido"}), 404 | |
| return jsonify(json.loads(open("./codes/"+code, "r").read())) | |
| def telegram(): | |
| if not os.path.exists("./telegram"): | |
| os.mkdir("./telegram") | |
| telegram = os.listdir("./telegram") | |
| tdata = [] | |
| for t in telegram: | |
| if not t.startswith("down_"): | |
| tdata.append({"name":t, "size":os.path.getsize("./telegram/"+t)}) | |
| return jsonify(tdata) | |
| def downloadtg(): | |
| filename = request.json.get('filename') | |
| code = str(uuid.uuid4().hex)[:8] | |
| savedata(code, {"current":0, "total":(os.path.getsize("./telegram/"+filename)//(20*1024*1024))+1, "token":None}) | |
| flush = BitFlush() | |
| thread = threading.Thread(target=flush.upload, args=("./telegram/"+filename, code)) | |
| thread.start() | |
| return jsonify({"code": code}), 202 | |
| if __name__ == '__main__': | |
| app.run(debug=True, host="0.0.0.0", threaded=True) |