def _append_json_logfile(self, args, status): """Write status to jsonfile in JSON format.""" self._set_json_logfile_name(args) status['time'] = int(time()) with open(self.jsonfile, 'a') as logfd: json = toJSON(status) logfd.write(json + '\n')
def return_sharedData(tree): identifier = 'window._sharedData = ' for a in tree.findall('.//script'): try: if a.text.startswith(identifier): try: return toJSON(a.text.replace(identifier, '')[:-1]) except Exception as e: print '\nError returning sharedData JSON: %s' % (e) except Exception as e: continue return False
def return_feedData(tree): identifier = "window.__additionalDataLoaded('feed'," for a in tree.findall('.//script'): try: if a.text.startswith(identifier): try: return toJSON(a.text.replace(identifier, '')[:-2]) except Exception as e: print('\nError returning sharedData JSON: %s' % (e)) except Exception as e: continue return None
def _write_package_json(self, args, status): """Write package status to a JSON file.""" package_json = self._get_package_json_filename(args) if os.path.isfile(package_json): with open(package_json, 'r') as infofd: try: info = fileToJSON(infofd) info['end'] = int(time()) except: return else: info = {'start': int(time())} for key in status: if key not in info: info[key] = status[key] with open(package_json, 'w') as infofd: json = toJSON(info, indent=4, sort_keys=True) infofd.write(json + '\n')
def do_GET(self): self.send_response(200) self.send_header('Content-type', 'application/json') self.send_header('Access-Control-Allow-Origin', '*') self.end_headers() if self.path != '/favicon.ico': db_dump = [] entities = { 'MAGA': 1, 'MCD': 2, 'MINDEF': 3, 'MINEDUC': 4, 'MEM': 5, 'MINFIN': 6, 'MINEX': 7, 'MSPAS': 8, 'MINTRAB': 9, 'USAC': 10 } myclient = ('PRIVATE-URL') mydb = myclient['Ministerio'] mycol = mydb['Grafica'] opts = self.path.split('/') if opts[2] == '1': for item in mycol.find({'_id': entities[opts[1]] * 10 + 1}, { '_id': 0, opts[1]: 1 }): db_dump.append(['Detalle', 'Egreso']) for i in range(6): db_dump.append([ item[opts[1]][i]['detalle'], float(item[opts[1]][i]['egreso']) ]) elif opts[2] == '2': for item in mycol.find({'_id': entities[opts[1]] * 10 + 2}, { '_id': 0, opts[1]: 1 }): for i in range(6): db_dump.append( float(item[opts[1]][i]['asignado']) / 1000) elif opts[2] == '4': for item in mycol.find( {'_id': str(entities[opts[1]]) + '-4-10'}, { '_id': 0, opts[1]: 1 }): db_dump.append(['Detalle', 'Ingreso']) for i in range(6): db_dump.append([ item[opts[1]][i]['detalle'], float(item[opts[1]][i]['ingreso']) ]) if opts[2] == '5': for item in mycol.find({'_id': entities[opts[1]] * 10 + 5}, { '_id': 0, opts[1]: 1 }): db_dump.append([ 'Location', 'Parent', 'Market trade volume (size)', 'Market increase/decrease (color)' ]) db_dump.append(['Proveedores', None, 0, 0]) for i in range(6): db_dump.append([ item[opts[1]][i]['proveedor'], 'Proveedores', float(item[opts[1]][i]['debito']), -i * 10 ]) else: pass self.wfile.write(toJSON(db_dump).encode('utf-8'))
def log_in(self): # signs the user into instagram self.logged_in = False try: self.logger.log('\nTrying to sign in as %s: \,' % (self.params['username'])) # extract cookies from Instagram main page extract = self.browser.get(self.insta_urls['domain']) csrftoken = re.search('(?<=\"csrf_token\":\")\w+', extract.text).group(0) self.browser.headers.update({'X-CSRFToken': csrftoken}) sleep(2) # extract csrf token from login requests post extract_login = self.browser.post(self.insta_urls['login'], data={ 'username': self.params['username'], 'password': self.params['password'] }, allow_redirects=True) self.csrf_token = extract_login.cookies['csrftoken'] self.browser.headers.update({'X-CSRFToken': self.csrf_token}) self.browser.cookies['ig_vw'] = '1536' self.browser.cookies['ig_pr'] = '1.25' self.browser.cookies['ig_vh'] = '772' self.browser.cookies['ig_or'] = 'landscape-primary' sleep(2) # check if login successful if extract_login.ok: if self.check_login(): self.logger.log('Success') self.logged_in = True sleep(2) else: self.logger.log('\nError signing in.\,') self.logger.log( 'Request returns %s but it seems it didn\'t work.' % (extract_login.status_code)) response_data = toJSON(extract_login.text) self.logger.log('\nInstagram error message: %s' % (response_data.get('message'))) if response_data.get('error_type'): self.logger.log('\nError type: %s' % (response_data.get('error_type'))) exit('\nCheck you entered the correct details!\n') else: self.logger.log('\nError signing in.\,') self.logger.log('Request returns %s error.' % (extract_login.status_code)) response_data = toJSON(extract_login.text) self.logger.log('\nInstagram error message: %s' % (response_data.get('message'))) if (response_data.get('message') == 'checkpoint_required'): self.logger.log('\nCheckpoint_url: %s' % (response_data.get('checkpoint_url'))) self.logger.log( '\nOpen Instagram on your desktop browser and confirm that it is you trying to log in!' ) self.logger.log('You have 30 seconds...') sleep(30) elif response_data.get('error_type'): self.logger.log('\nError type: %s' % (response_data.get('error_type'))) exit('\nCheck you entered the correct details!\n') else: exit('\nCheck you entered the correct details!\n') except KeyError as e: self.logger.log( '\nERROR signing in, InstaBot probably failed to obtain Instagram\'s cookies' ) self.logger.log(traceback.format_exc()) exit('\nApologies, I could not sign you in.\n') except Exception as e: self.logger.log('\nERROR while attempting sign in: %s\n' % e) self.logger.log(traceback.format_exc()) exit('\nApologies, I could not sign you in.\n')