diff --git a/tiktok_downloader/__main__.py b/tiktok_downloader/__main__.py index 7df3c9b..4c124b9 100644 --- a/tiktok_downloader/__main__.py +++ b/tiktok_downloader/__main__.py @@ -3,8 +3,10 @@ import os import sys import requests -from .ssstik import ssstik -from .snaptik import snaptik +from .ssstik import Ssstik +from .snaptik import Snaptik +from .mdown import Mdown +from .tikmate import Tikmate from .scrapper import info_post from sys import stderr @@ -15,8 +17,10 @@ ) arg.add_argument('--snaptik', action='store_true') arg.add_argument('--ssstik', action='store_true') +arg.add_argument('--mdown', action='store_true') +arg.add_argument('--tikmate', action='store_true') arg.add_argument('--info', action='store_true') -arg.add_argument('--url', type=str, required=True) +arg.add_argument('--url', type=str) arg.add_argument('--server', action='store_true') arg.add_argument('--host', type=str, default='127.0.0.1') arg.add_argument('--debug', action='store_true') @@ -28,72 +32,76 @@ from .server import app app.run(host=parse.host, port=parse.port, debug=bool(parse.debug)) elif parse.url: - if parse.snaptik or parse.ssstik: - try: - ok = snaptik( - parse.url - ).get_media() if parse.snaptik else ssstik().get_media( - parse.url - ) - if parse.json or not(parse.save): - print( - json.dumps( - [ - { - 'type': i.type, - 'url': i.json - } for i in ok - ], - indent=4 - ) - ) - elif parse.save: - ok[0].download(parse.save) - else: - os.system("python3 -m tiktok_downloader --help") - except Exception as e: - print(e) - stderr.write('Post Not Found\n') - stderr.flush() - sys.exit(1) - elif parse.info: - try: - resp = info_post(parse.url) + service = ( + ( + parse.snaptik and Snaptik + ) or ( + parse.ssstik and Ssstik + ) or ( + parse.tikmate and Tikmate + ) or ( + parse.mdown and Mdown + ) or Snaptik + ) + try: + ok = service(parse.url) + if parse.json or not(parse.save): print( json.dumps( - { - 'account': { - 'avatar': resp.account.avatar, - 'username': resp.account.username, - 'nickname': resp.account.nickname, - 'signature': resp.account.signature, - 'create': ( - ( - resp.account.create - and - resp.account.create.timestamp() - ) or 0), - 'verified': resp.account.verified - }, - 'music': resp.music, - 'cover': resp.cover, - 'caption': resp.caption, - 'create': resp.create.timestamp(), - 'url': resp.url, - 'id': resp.id - }, + [ + { + 'type': i.type, + 'url': i.json + } for i in ok + ], indent=4 ) ) - except requests.exceptions.ConnectionError: - stderr.write('[x] offline\n') - stderr.flush() - sys.exit(1) - except (KeyError, AttributeError): - stderr.write('Post Not Found\n') - stderr.flush() - sys.exit(1) - else: - os.system("python3 -m tiktok_downloader --help") + elif parse.save: + ok[0].download(parse.save) + else: + os.system("python3 -m tiktok_downloader --help") + except Exception as e: + print(e) + stderr.write('Post Not Found\n') + stderr.flush() + sys.exit(1) +elif parse.info: + try: + resp = info_post(parse.url) + print( + json.dumps( + { + 'account': { + 'avatar': resp.account.avatar, + 'username': resp.account.username, + 'nickname': resp.account.nickname, + 'signature': resp.account.signature, + 'create': ( + ( + resp.account.create + and + resp.account.create.timestamp() + ) or 0), + 'verified': resp.account.verified + }, + 'music': resp.music, + 'cover': resp.cover, + 'caption': resp.caption, + 'create': resp.create.timestamp(), + 'url': resp.url, + 'id': resp.id + }, + indent=4 + ) + ) + except requests.exceptions.ConnectionError: + stderr.write('[x] offline\n') + stderr.flush() + sys.exit(1) + except (KeyError, AttributeError): + stderr.write('Post Not Found\n') + stderr.flush() + sys.exit(1) else: os.system("python3 -m tiktok_downloader --help") diff --git a/tiktok_downloader/__pycache__/__init__.cpython-39.pyc b/tiktok_downloader/__pycache__/__init__.cpython-39.pyc index 2af88a1..919d1c5 100644 Binary files a/tiktok_downloader/__pycache__/__init__.cpython-39.pyc and b/tiktok_downloader/__pycache__/__init__.cpython-39.pyc differ diff --git a/tiktok_downloader/__pycache__/__main__.cpython-39.pyc b/tiktok_downloader/__pycache__/__main__.cpython-39.pyc index 8ae82b4..9a38385 100644 Binary files a/tiktok_downloader/__pycache__/__main__.cpython-39.pyc and b/tiktok_downloader/__pycache__/__main__.cpython-39.pyc differ diff --git a/tiktok_downloader/__pycache__/decoder.cpython-39.pyc b/tiktok_downloader/__pycache__/decoder.cpython-39.pyc index 349c528..1c066a6 100644 Binary files a/tiktok_downloader/__pycache__/decoder.cpython-39.pyc and b/tiktok_downloader/__pycache__/decoder.cpython-39.pyc differ diff --git a/tiktok_downloader/__pycache__/mdown.cpython-39.pyc b/tiktok_downloader/__pycache__/mdown.cpython-39.pyc index 6bc0759..0953203 100644 Binary files a/tiktok_downloader/__pycache__/mdown.cpython-39.pyc and b/tiktok_downloader/__pycache__/mdown.cpython-39.pyc differ diff --git a/tiktok_downloader/__pycache__/server.cpython-39.pyc b/tiktok_downloader/__pycache__/server.cpython-39.pyc new file mode 100644 index 0000000..6993fe6 Binary files /dev/null and b/tiktok_downloader/__pycache__/server.cpython-39.pyc differ diff --git a/tiktok_downloader/__pycache__/snaptik.cpython-39.pyc b/tiktok_downloader/__pycache__/snaptik.cpython-39.pyc index 13a4461..be812b9 100644 Binary files a/tiktok_downloader/__pycache__/snaptik.cpython-39.pyc and b/tiktok_downloader/__pycache__/snaptik.cpython-39.pyc differ diff --git a/tiktok_downloader/__pycache__/ssstik.cpython-39.pyc b/tiktok_downloader/__pycache__/ssstik.cpython-39.pyc index 4f9fe1f..de15c0d 100644 Binary files a/tiktok_downloader/__pycache__/ssstik.cpython-39.pyc and b/tiktok_downloader/__pycache__/ssstik.cpython-39.pyc differ diff --git a/tiktok_downloader/__pycache__/tikmate.cpython-39.pyc b/tiktok_downloader/__pycache__/tikmate.cpython-39.pyc index 6f409cc..b90dbec 100644 Binary files a/tiktok_downloader/__pycache__/tikmate.cpython-39.pyc and b/tiktok_downloader/__pycache__/tikmate.cpython-39.pyc differ diff --git a/tiktok_downloader/decoder.py b/tiktok_downloader/decoder.py index dd12c3a..42ccf82 100644 --- a/tiktok_downloader/decoder.py +++ b/tiktok_downloader/decoder.py @@ -5,16 +5,19 @@ alpha = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/" + def search(d: Union[str, list], q: Union[str, int]): try: return d.index(q) - except: + except Exception: return -1 + def main(d, e, f): g = list(alpha) h = g[0:e] i = g[0:f] + def freduce(a, b, c): if search(h, b) != -1: a += search(h, b) * (e ** c) @@ -26,16 +29,22 @@ def freduce(a, b, c): j = int((j - (j % f)) / f) return int(k) or 0 -def reduces(function, iterable, initializer = None): #modified reduce function like js :https://www.geeksforgeeks.org/reduce-in-python/ + +def reduces(function, iterable, initializer=None): + """ + [!] modified reduce function like js + :https://www.geeksforgeeks.org/reduce-in-python/ + """ it = iter(iterable) if initializer is None: value = next(it) else: value = function(initializer, next(it), 0) - for index, element in enumerate(it, 1): + for index, element in enumerate(it, 1): value = function(value, element, index) return value + def decoder(h, u, n, t, e, r): r = "" i = 0 @@ -43,10 +52,9 @@ def decoder(h, u, n, t, e, r): s = "" while h[i] != n[e]: s += h[i] - i+=1 + i += 1 for j in range(n.__len__()): s = s.replace(n[j], j.__str__()) r += chr(main(s, e, 10) - t) i += 1 return r - diff --git a/tiktok_downloader/mdown.py b/tiktok_downloader/mdown.py index 9d644ae..5ac976b 100644 --- a/tiktok_downloader/mdown.py +++ b/tiktok_downloader/mdown.py @@ -53,3 +53,7 @@ def get_media(self, url: str): 'html.parser' ).find_all('a', attrs={'target': '_blank'}) ] + + +def Mdown(url: str): + return mdown().get_media(url) diff --git a/tiktok_downloader/server.py b/tiktok_downloader/server.py index 55a67a3..6b4f53a 100644 --- a/tiktok_downloader/server.py +++ b/tiktok_downloader/server.py @@ -1,19 +1,18 @@ -from . import mdown from flask import ( Flask, request, render_template, Response ) -from . import ( - info_post, - snaptik, - ssstik, - tikmate -) +from .mdown import Mdown +from .snaptik import Snaptik +from .ssstik import Ssstik +from .tikmate import Tikmate +from .scrapper import info_post import json import os + app = Flask( __name__, template_folder=os.path.abspath(__file__+'/../templates'), @@ -76,15 +75,13 @@ def snapt(path): return json.dumps({'msg': 'path tidak ditemukan'}) if request.args.get('url'): try: - res = [] - if path == 'snaptik': - res = snaptik(request.args['url']).get_media() - elif path == 'ssstik': - res = ssstik().get_media(request.args['url']) - elif path == 'tikmate': - res = tikmate().get_media(request.args['url']) - elif path == 'mdown': - res = mdown().get_media(request.args['url']) + service = ({ + 'snaptik': Snaptik, + 'ssstik': Ssstik, + 'tikmate': Tikmate, + 'mdown': Mdown + }).get(path, Snaptik) + res = service(request.args['url']) return Response( json.dumps( [ diff --git a/tiktok_downloader/snaptik.py b/tiktok_downloader/snaptik.py index f724d8e..48d960d 100644 --- a/tiktok_downloader/snaptik.py +++ b/tiktok_downloader/snaptik.py @@ -4,7 +4,6 @@ from .Except import InvalidUrl from requests import Session from re import findall -from os.path import dirname from .decoder import decoder @@ -46,7 +45,7 @@ def get_media(self) -> list[info_videotiktok]: self.resp.text )[0] )) - + stderr.flush() return [ info_videotiktok( @@ -66,3 +65,7 @@ def get_media(self) -> list[info_videotiktok]: def __iter__(self): yield from self.get_media() + + +def Snaptik(url: str): + return snaptik(url).get_media() diff --git a/tiktok_downloader/ssstik.py b/tiktok_downloader/ssstik.py index a5604e4..19f3476 100644 --- a/tiktok_downloader/ssstik.py +++ b/tiktok_downloader/ssstik.py @@ -101,3 +101,7 @@ def get_media(self, url: str) -> list[info_videotiktok]: return hasil except IndexError: raise InvalidURL() + + +def Ssstik(url: str, delay=10): + return ssstik(delay).get_media(url) diff --git a/tiktok_downloader/tikmate.py b/tiktok_downloader/tikmate.py index 7d1178b..ca4e355 100644 --- a/tiktok_downloader/tikmate.py +++ b/tiktok_downloader/tikmate.py @@ -51,3 +51,7 @@ def get_media(self, url: str) -> list[info_videotiktok]: decode ) ] + + +def Tikmate(url: str): + return tikmate().get_media(url)