def login(): try: driver = webdriver.Firefox() driver.get('https://www.instagram.com/accounts/login') curl = driver.current_url while driver.current_url == curl: input(' Are you done? [PRESS ENTER] ') # sleep(1) cookies = driver.get_cookies() driver.quit() return {c['name']: c for c in list(map(conv_cookie, cookies))} except Exception: misc.print_wrn('Login', 'Unable to retrieve cookies ... ') return {}
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 man(*args): global __driver if __driver: __driver.quit() __driver = webdriver.Firefox() __driver.get('https://instagram.com/') if __bot: for c in __bot.cookies.values(): __driver.add_cookie(c) __driver.refresh() else: misc.print_wrn( 'man', 'I\'ll let you decide how sensible this is without being logged in ... ' ) return SUCCESS
def get_followersr(self, userid, nxmi=None): res = self.session.get(urls.FOLLOWERS.format( json.dumps({ 'id': userid, 'first': 50, 'after': nxmi, })), headers=self.headers).json() if res['status'] != 'ok': misc.print_wrn('get_followersr({})'.format(userid), json.dumps(res)) return [] res = res['data']['user']['edge_followed_by'] if res['page_info']['has_next_page']: return [ *self.get_followersr(userid, res['page_info']['end_cursor']), *list(map(lambda e: e['node'], res['edges'])) ] return list(map(lambda e: e['node'], res['edges']))
def get_media(username, dest=None, sess=None): userid = get_userid(username) variables = {'id': userid, 'first': 50, 'after': None} if not dest: dest = os.path.abspath( os.path.join(params.TMP_PATH, '{}/m{}'.format(username, str(time.time())))) loggedin = True if not sess: sess = req.Session() loggedin = False while True: res = sess.get(urls.MEDIA.format(json.dumps(variables))).json() if res['status'] != 'ok': break res = res['data']['user']['edge_owner_to_timeline_media'] if len(res['edges']) == 0: misc.print_wrn( 'get', 'No media found! Maybe the account is private? {}'.format( '' if loggedin else '(sign in and try again)')) return if not os.path.isdir(dest): try: os.makedirs(dest) except Exception: misc.print_wrn( 'get', 'Couldn\'t create directory "{}" --> aborting ... '.format( dest)) return for e in res['edges']: if 'edge_sidecar_to_children' in e['node'].keys(): for i, c in enumerate( e['node']['edge_sidecar_to_children']['edges']): down_img(c['node'], dest, sess, e['node']['shortcode'], '-{:02d}'.format(i)) else: down_img(e['node'], dest, sess) variables['after'] = res['page_info']['end_cursor'] if not variables['after']: break
def down_img(node, dest, sess=None, shc='', sffx=''): if not sess: sess = req.Session() if 'shortcode' not in node.keys(): node['shortcode'] = shc p = os.path.abspath( os.path.join( dest, '{}{}.{}'.format(node['shortcode'], sffx, 'mp4' if node['is_video'] else 'jpg'))) print(' Getting "{}" --> "{}"'.format(node['shortcode'], p)) try: with open(p, 'wb') as f: if node['is_video']: r = sess.get(node['video_url']) for ch in r.iter_content(chunk_size=255): if ch: f.write(ch) else: f.write(sess.get(node['display_url']).content) except Exception as err: misc.print_wrn( 'get', 'Failed to retrieve post "{}" ... '.format(node['shortcode'])) misc.print_wrn('get', str(err))
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