def __parse_cmd(cmd: str) -> None: args = cmd.split(' ') c = __get_cmd(args[0]) if c: c.exec(args[1:]) return if not args[0].strip(): return misc.print_err('Unknown command "{}" ...'.format(args[0]))
def exec(self, args: List[str]) -> None: prsr = ArgumentParser(usage='cd <dirname>') prsr.add_argument(dest='d', nargs='?', help='The new current working directory ... ') try: ar = prsr.parse_args(args) except SystemExit: return if not ar.d: misc.print_err('Usage: cd <dirname>') return if not self.cl.cd(ar.d): misc.print_err('Unknown directory "{}" ...'.format(args[0]))
def ls(*args): uname = None if len(args) == 2 and args[1] in ('?', 'help'): misc.print_dict('Help: "{} <opt.>"'.format(args[0]), HELP['ls, dir, list']['opts']) return SUCCESS elif len(args) == 2 and args[1].startswith('-u'): uname = args[1][2:] d = os.path.join(params.TMP_PATH, uname) if not os.path.isdir(d): misc.print_err('ls', 'User hasn\'t been scraped yet!') return FAILURE for u in [ d for d in os.listdir(params.TMP_PATH) if os.path.isdir(os.path.join(params.TMP_PATH, d)) ]: if uname and u != uname: continue print(' {}{}{}{}{}{}'.format( cm.Style.BRIGHT, cm.Fore.LIGHTGREEN_EX, u, '' if len(args) == 2 and args[1] in ('u', 'users') else ':', cm.Fore.RESET, cm.Style.RESET_ALL)) ds = [ d for d in os.listdir(os.path.join(params.TMP_PATH, u)) if os.path.isdir(os.path.join(params.TMP_PATH, u, d)) ] mgs = list(filter(lambda d: d.startswith('m'), ds)) ogs = list(filter(lambda d: d.startswith('o'), ds)) igs = list(filter(lambda d: d.startswith('i'), ds)) if (len(args) < 2 or uname or args[1] in ('m', 'media')) and len(mgs) > 0: print(' \tMedia: ') for mg in mgs: print( ' \t -> Grab: %s' % datetime.datetime.fromtimestamp(float(mg[1:])).isoformat()) if (len(args) < 2 or uname or args[1] in ('o', 'followers')) and len(ogs) > 0: print(' \tFollowers: ') for og in ogs: print( ' \t -> Grab: %s' % datetime.datetime.fromtimestamp(float(og[1:])).isoformat()) if (len(args) < 2 or uname or args[1] in ('i', 'following')) and len(igs) > 0: print(' \tFollowing: ') for ig in igs: print( ' \t -> Grab: %s' % datetime.datetime.fromtimestamp(float(ig[1:])).isoformat()) return SUCCESS
def browse(*args): cfile = os.path.join(params.SRV_PATH, 'config/conf.json') if not os.path.isfile(cfile): misc.print_err(args[0], '"{}" doesn\'t exist!'.format(cfile)) return FAILURE with open(cfile, 'r') as f: conf = json.load(f) try: print(' Listening on http://localhost:%d ... ' % conf['port']) webbrowser.open('http://localhost:{}'.format(conf['port'])) except KeyError: misc.print_err(args[0], 'Error: Malformed configuration file!') return SUCCESS
def __init(host: str, port: int = 4800) -> None: global __client, CMDS if misc.unix(): readline.parse_and_bind('tab: complete') readline.set_completer(__cmd_compl) else: misc.print_wrn( 'Consider using a UNIX-based OS for tab-completion ... ') try: __client = client.Client(host, port) for k, v in CMDS.items(): CMDS[k] = v(__client) except Exception as e: misc.print_err(str(e)) os._exit(1)
def exec(self, args: List[str]) -> None: prsr = ArgumentParser(usage='put <local-file> [<to-file>]') prsr.add_argument(dest='f', type=str, nargs='?', help='The local file name ... ') prsr.add_argument(dest='t', type=str, nargs='?', help='The target remote file name ... ') try: ar = prsr.parse_args(args) except SystemExit: return if not ar.f: misc.print_err('Usage: put <local-file> [<to-file>]') return ulp = self.cl.ul(ar.f, ar.t) if not ulp: misc.print_err('Upload failed!') return print(' Uploaded to "%s" ... ' % ulp)
def exec(self, args: List[str]) -> None: prsr = ArgumentParser(usage='mkdir <dirname1> [<dirname2> ...]') prsr.add_argument(dest='d', type=str, nargs='*', help='The name of the directory to-be-created ...') try: ar = prsr.parse_args(args) except SystemExit: return if not ar.d: misc.print_err('Usage: mkdir <dirname1> [<dirname2> ...]') return for d in ar.d: if not self.cl.mkdir(d): misc.print_err('Couldn\'t create "{}" ... '.format(d))
def handle(com, debug=False): try: for k in COMS.keys(): if com[0] in k: rc = COMS[k](*com) if rc == CLOSING: __cleanup() return rc if com[0] == '': return SUCCESS misc.print_err('d4v1d', 'Unknown command: "{}"'.format(com[0])) return UNKNOWN except Exception as e: if debug: misc.print_err(str(e.__class__.__name__), str(e)) traceback.print_exc() return FAILURE
def login(*args): global __bot if len(args) > 1: if args[1] in ('renew', 'r'): __bot = bot.Bot(cache=False) elif args[1] in ('help', '?'): misc.print_dict('Help: "{} [<opt.>]'.format(args[0]), HELP['login']['opts']) else: misc.print_err(args[0], 'Unknown option "{}"'.format(args[1])) return FAILURE else: __bot = bot.Bot() if __bot and not __bot.cookies: misc.print_err('Bot', 'Creation failed!') __bot = None return FAILURE return SUCCESS
def exec(self, args: List[str]) -> None: prsr = ArgumentParser(usage='ls [-lat] [<dirname>]') prsr.add_argument(dest='d', type=str, nargs='?', help='Target directory\'s name ... ') prsr.add_argument('-l', dest='l', action='store_true', help='Display as list ... ') prsr.add_argument('-a', dest='a', action='store_true', help='Show all files ... ') prsr.add_argument('-t', dest='t', action='store_true', help='Also show detected MIME-Types ... ') try: ar = prsr.parse_args(args) except SystemExit: return _dir = self.cl.ls(ar.d) if type(_dir) != list: misc.print_err('Unknown directory "{}" ...'.format(ar.d)) return _dir = list(filter(lambda f: not f['name'].startswith('.') or ar.a, _dir)) if ar.l: misc.print_table(list(map(lambda f: [ 'd' if f['type'] == 'dir' else '-', f['type'] if ar.t else '', f['name'] ], _dir))) else: print(' '.join(map(lambda f: f['name'] if f['type'] != 'dir' else '{}{}{}'.format(cr.Fore.LIGHTCYAN_EX, f['name'], cr.Fore.RESET), _dir)))
def show(*args): if len(args) < 2: misc.print_err(args[0], 'Missing arguments "{} <opt.>"'.format(args[0])) return FAILURE elif args[1] in ('u', 'user'): if __bot: misc.print_dict('Current User', { 'Username': __bot.user['username'], 'User-ID': __bot.user['pk'], }) else: print(' Not logged in!') return FAILURE elif args[1] in ('h', 'headers'): if __bot: misc.print_dict('Header-Config.', __bot.headers) else: print(' Not configured. Try logging in!') return FAILURE elif args[1] in ('a', 'user-agent'): if __bot: misc.print_dict('Header-Config.', { 'User-Agent': __bot.headers['User-Agent'], }) else: print(' Not configured. Try logging in!') return FAILURE elif args[1] in ('c', 'cookies'): if __bot: misc.print_dict('Cookies', {k: v['value'] for k, v in __bot.cookies.items()}) else: print(' None set. Try logging in!') elif args[1] in ('help', '?'): misc.print_dict('Help: "{} <opt.>"'.format(args[0]), HELP['show, display, sh, disp']['opts']) else: misc.print_err(args[0], 'Unkown option "{}"'.format(args[1])) return FAILURE return SUCCESS
def exec(self, args: List[str]) -> None: prsr = ArgumentParser(usage='get <filename> [<localname>]') prsr.add_argument(dest='f', type=str, nargs='?', help='The target filename ... ') prsr.add_argument(dest='p', type=str, nargs='?', help='The local path ... ', default='') try: ar = prsr.parse_args(args) except SystemExit: return if not ar.f: misc.print_err('Usage: get <file-path>') return dlp = self.cl.dl(ar.f, ar.p) if not dlp: misc.print_err('File not found!') return print(' Downloaded to "%s" ... ' % dlp)
import os, threading, json, logging from flask import Flask, request, Response, send_from_directory, render_template from flup.server.fcgi import WSGIServer import requests as req from lib import params, misc LOG = logging.getLogger('werkzeug') LOG.disabled = True CONF = {} PUBLIC = os.path.abspath(os.path.join(params.SRV_PATH, 'public')) TEMPLT = os.path.abspath(os.path.join(params.SRV_PATH, 'templates')) if not os.path.isfile(os.path.join(params.SRV_PATH, 'config/conf.json')): misc.print_err( 'server', 'Missing configuration file! Supposed to be at "{}" ... '.format( os.path.abspath(os.path.join(params.SRV_PATH, 'config/conf.json')))) os._exit(1) with open(os.path.join(params.SRV_PATH, 'config/conf.json'), 'r') as f: CONF = json.load(f) __app = Flask(__name__, static_url_path='/', static_folder=PUBLIC, template_folder=TEMPLT) __app.logger.disabled = True __server = None def not_scraped():
def rm(*args): if len(args) < 2: misc.print_err(args[0], 'Missing arguments "{} <opt.>"'.format(args[0])) return FAILURE elif args[1] in ('?', 'help'): misc.print_dict('Help: "{} <opt.>"'.format(args[0]), HELP['rm, del, remove, delete']['opts']) elif args[1] in ('p', 'purge'): count = 0 size = 0 for u in args[2].split(',') if len(args) >= 3 else os.listdir( params.TMP_PATH): ud = os.path.join(params.TMP_PATH, u) if not os.path.isdir(ud): continue print(' Purging "{}{}{}": '.format(cm.Fore.LIGHTGREEN_EX, u, cm.Fore.RESET), end='') ds = os.listdir(ud) print(' Media', end='') mfs = list(filter(lambda d: d.startswith('m'), ds)) mfs.sort() for m in mfs[:-1]: size += sum([ os.path.getsize(os.path.join(ud, m, f)) for f in os.listdir(os.path.join(ud, m)) if os.path.isfile(os.path.join(ud, m, f)) ]) count += 1 shutil.rmtree(os.path.join(ud, m)) print('✔️, Followers', end='') ofs = list(filter(lambda d: d.startswith('o'), ds)) ofs.sort() first = 0 for i, o in enumerate(ofs[::-1]): if os.path.isfile(os.path.join(ud, o, 'followers.json')): first = len(ofs) - i - 1 break for o in [*ofs[:first], *ofs[first + 1:]]: size += sum([ os.path.getsize(os.path.join(ud, o, f)) for f in os.listdir(os.path.join(ud, o)) if os.path.isfile(os.path.join(ud, o, f)) ]) count += 1 shutil.rmtree(os.path.join(ud, o)) print('✔️, Following', end='') ifs = list(filter(lambda d: d.startswith('i'), ds)) ifs.sort() first = 0 for j, i in enumerate(ifs[::-1]): if os.path.isfile(os.path.join(ud, i, 'following.json')): first = len(ifs) - j - 1 break for i in [*ifs[:first], *ifs[first + 1:]]: size += sum([ os.path.getsize(os.path.join(ud, i, f)) for f in os.listdir(os.path.join(ud, i)) if os.path.isfile(os.path.join(ud, i, f)) ]) count += 1 shutil.rmtree(os.path.join(ud, i)) print('✔️') print( ' Deleted {}{}{} directories, freeing up {}{:,.3f}{} MiB of space!' .format(cm.Style.BRIGHT, count, cm.Style.RESET_ALL, cm.Style.BRIGHT, size / 2**20, cm.Style.RESET_ALL)) return SUCCESS
def get(*args): if len(args) < 2: misc.print_err(args[0], 'Missing arguments "{} <opt.>"'.format(args[0])) return FAILURE elif args[1] in ('?', 'help'): misc.print_dict('Help: "{} <opt.>"'.format(args[0]), HELP['get, dump']['opts']) else: if len(args) < 3: misc.print_err( args[0], 'Missing arguments "{} {} <user>"'.format(args[0], args[1])) elif args[1] in ('v', 'overview'): misc.print_dict( 'Overview: {}'.format(args[2]), { k.title().replace('_', ' '): v for k, v in api.get_user_overview( args[2], headers=__bot.headers if __bot else {}).items() }) elif args[1] in ('o', 'followers'): if not __bot: misc.print_err(args[0], 'You need to be logged in!') return FAILURE fs = __bot.get_followers(args[2]) if len(fs) == 0: misc.print_wrn( args[0], 'No followers found. Maybe the account is private?') return FAILURE print(' Downloaded {}{}{} followers.'.format( cm.Style.BRIGHT, len(fs), cm.Style.RESET_ALL)) elif args[1] in ('i', 'following'): if not __bot: misc.print_err(args[0], 'You need to be logged in!') return FAILURE fs = __bot.get_following(args[2]) if len(fs) == 0: misc.print_wrn( args[0], 'No following found. Maybe the account is private?') return FAILURE print(' Downloaded {}{}{} following-connections.'.format( cm.Style.BRIGHT, len(fs), cm.Style.RESET_ALL)) elif args[1] in ('m', 'media'): api.get_media(args[2], None, __bot.session if __bot else None) else: if len(args) < 4: misc.print_err( 'get', 'Missing arguments "{} {} {} <user2>"'.format(*args)) elif args[1] in ('p', 'path'): u1, u2 = args[2:] if not __bot: misc.print_err(args[0], 'You need to be logged in!') return FAILURE shp = __bot.shortest_path(u2, u1) print(shp[0][1] + ''.join([ ' {} '.format('->' if i[0] else '<-') + i[1] for i in shp[1:] ])) return SUCCESS