def shorten(self, long_url): goog = Googl() resp = goog.shorten(long_url) if resp.get('error'): error = resp['error']['message'] self.logger.warning("Failed to shorten %s - %s", long_url, error) return long_url short_url = resp['id'] return short_url
class SavedNotification(object): def __init__(self, sender_instance): self.sender_instance = sender_instance self.sender_type = ContentType.objects.get_for_model(self.sender_instance.__class__) self.url = "http://%s%s" % (Site.objects.get_current(), self.sender_instance.get_absolute_url()) self.msg = self.sender_instance.twitter_message if hasattr(self.sender_instance, 'twitter_message') else str(self.sender_instance) self.googl = Googl(getattr(settings, 'GOOGL_KEY', None)) self.auth = OAuth(settings.TWEET_SAVED_OAUTH_TOKEN, settings.TWEET_SAVED_OAUTH_TOKEN_SECRET, CONSUMER_KEY, CONSUMER_SECRET, ) self.twitter = Twitter(auth=self.auth) def tweet(self): if not TweetedObject.objects.filter(content_type=self.sender_type, object_id=self.sender_instance.pk).exists(): url = self.googl.shorten(self.url)['id'] max_message_length = TWITTER_LENGTH - (len(url) + 1) message = "%s %s" % (self.msg[:max_message_length], url) response = self.twitter.statuses.update(status=message) print response self._remember_tweet(response) def _remember_tweet(self, response): t = TweetedObject(status_id=response['id'], content_object=self.sender_instance, screen_name=response['user']['screen_name']) t.save()
def __init__(self, sender_instance): self.sender_instance = sender_instance self.sender_type = ContentType.objects.get_for_model(self.sender_instance.__class__) self.url = "http://%s%s" % (Site.objects.get_current(), self.sender_instance.get_absolute_url()) self.msg = self.sender_instance.twitter_message if hasattr(self.sender_instance, 'twitter_message') else str(self.sender_instance) self.googl = Googl(getattr(settings, 'GOOGL_KEY', None)) self.auth = OAuth(settings.TWEET_SAVED_OAUTH_TOKEN, settings.TWEET_SAVED_OAUTH_TOKEN_SECRET, CONSUMER_KEY, CONSUMER_SECRET, ) self.twitter = Twitter(auth=self.auth)
def table(data): try: googlkey = readconfig('googl') g = Googl(googlkey) shorten = True except: googlkey = None shorten = False metatable = PrettyTable() metafields = collections.OrderedDict() if arguments['report']: if not arguments['ip'] and not arguments['domain']: if arguments['url']: metafields['URL'] = data['url'] elif arguments['file'] or arguments['hash']: metafields['MD5'] = data['md5'] metafields['SHA1'] = data['sha1'] metafields['SHA256'] = data['sha256'] if int(data['positives']) > (int(data['total']) / 2): c = red else: c = green detectionratio = '{0}/{1}'.format(data['positives'], data['total']) metafields['Detection ratio'] = '{0}'.format(detectionratio) metafields['Analysis date'] = data['scan_date'] metafields['Scan id'] = data['scan_id'] if shorten: link = g.shorten(data['permalink'])['id'] else: link = data['permalink'] metafields['Link'] = link for f in metafields: col = green if f == 'Detection ratio': col = c metatable.add_row([ colorize(colorize(f, blue), bold), colorize(str(metafields[f]), col) ]) metatable.align = "l" metatable.header = False print metatable scans = data['scans'] scanstable = PrettyTable( colorize( colorize(['Engine', 'Detected', 'Result', 'Detail'], blue), bold)) for key in scans.keys(): engine = key detected = scans[key]['detected'] result = scans[key]['result'] if 'detail' in scans[key]: if shorten: detail = g.shorten(scans[key]['detail'])['id'] else: detail = scans[key]['detail'] else: detail = None if detected: scanstable.add_row( colorize([engine, detected, result, detail], red)) else: scanstable.add_row( colorize([engine, detected, result, detail], green)) scanstable.align = "l" print scanstable elif arguments['ip'] or arguments['domain']: if arguments['ip']: headtype = 'Hostname' headtype2 = 'hostname' if 'asn' in data: metafields['AS owner'] = data['as_owner'] metafields['ASN'] = data['asn'] metafields['Country'] = data['country'] for f in metafields: metatable.add_row([ colorize(colorize(f, blue), bold), colorize(str(metafields[f]), green) ]) metatable.align = "l" metatable.header = False print metatable elif arguments['domain']: headtype = 'IP address' headtype2 = 'ip_address' cattable = PrettyTable( colorize(colorize(['Categories'], blue), bold)) for c in data['categories']: cattable.add_row([colorize(str(c), green)]) cattable.align = "l" print cattable if 'WOT domain info' in data: print 'WOT domain info' w = PrettyTable() for k in data['WOT domain info']: w.add_row([ colorize(colorize(str(k), blue), bold), colorize(str(data['WOT domain info'][k]), green) ]) w.align = "l" w.header = False print w if 'subdomains' in data: subtable = PrettyTable( colorize(colorize(['Subdomains'], blue), bold)) for s in data['subdomains']: subtable.add_row([colorize(str(s), green)]) subtable.align = "l" print subtable whoistable = PrettyTable( colorize(colorize(['Whois lookup'], blue), bold)) whoistable.add_row([data['whois']]) whoistable.align = "l" print whoistable if len(data['resolutions']) > 0: print 'Resolutions {0}'.format(len(data['resolutions'])) restable = PrettyTable( colorize(colorize([headtype, 'Last resolved'], blue), bold)) for ip in data['resolutions']: restable.add_row( colorize([ip[headtype2], ip['last_resolved']], green)) restable.align = "l" print restable if len(data['detected_urls']) > 0: print 'URLs {0}'.format(len(data['detected_urls'])) urltable = PrettyTable( colorize( colorize(['Analysis date', 'Detection ratio', 'URL'], blue), bold)) for u in data['detected_urls']: adate = u['scan_date'] positives = u['positives'] total = u['total'] url = u['url'] ratio = '{0}/{1}'.format(positives, total) if int(positives) > (int(total) / 2): c = red else: c = green urltable.add_row(colorize([adate, ratio, url], c)) urltable.align = "l" print urltable if 'detected_referrer_samples' in data: print 'Detected referrer samples {0}'.format( len(data['detected_referrer_samples'])) dreftable = PrettyTable( colorize(colorize(['SHA256', 'Detection ratio'], blue), bold)) for dref in data['detected_referrer_samples']: positives = dref['positives'] total = dref['total'] ratio = '{0}/{1}'.format(positives, total) shahash = dref['sha256'] if int(positives) > (int(total) / 2): c = red else: c = green dreftable.add_row(colorize([shahash, ratio], c)) dreftable.align = "l" print dreftable if 'detected_downloaded_samples' in data: print 'Detected downloaded samples {0}'.format( len(data['detected_downloaded_samples'])) ddowntable = PrettyTable( colorize( colorize( ['Analysis date', 'SHA256', 'Detection ratio'], blue), bold)) for ddown in data['detected_downloaded_samples']: adate = ddown['date'] positives = ddown['positives'] total = ddown['total'] ratio = '{0}/{1}'.format(positives, total) shahash = ddown['sha256'] if int(positives) > (int(total) / 2): c = red else: c = green ddowntable.add_row(colorize([adate, shahash, ratio], c)) ddowntable.align = "l" print ddowntable if 'detected_communicating_samples' in data: print 'Detected communicating samples {0}'.format( len(data['detected_communicating_samples'])) dcommtable = PrettyTable( colorize( colorize( ['Analysis date', 'SHA256', 'Detection ratio'], blue), bold)) for dcomm in data['detected_communicating_samples']: adate = dcomm['date'] positives = dcomm['positives'] total = dcomm['total'] ratio = '{0}/{1}'.format(positives, total) shahash = dcomm['sha256'] if int(positives) > (int(total) / 2): c = red else: c = green dcommtable.add_row(colorize([adate, shahash, ratio], c)) dcommtable.align = "l" print dcommtable elif arguments['scan'] and not arguments['--rescan']: if arguments['url']: metafields['URL'] = data['url'] elif arguments['file']: metafields['MD5'] = data['md5'] metafields['SHA1'] = data['sha1'] metafields['SHA256'] = data['sha256'] metafields['Scan id'] = data['scan_id'] if shorten: link = g.shorten(data['permalink'])['id'] else: link = data['permalink'] metafields['Link'] = link for f in metafields: metatable.add_row([ colorize(colorize(f, blue), bold), colorize(str(metafields[f]), green) ]) metatable.align = "l" metatable.header = False print metatable time.sleep(30) arguments['scan'] = False arguments['report'] = True arguments['<resource>'] = data['scan_id'] key = readconfig('virustotal') vtc = Virustotal(key) if arguments['url']: output(vtc.urlReport(arguments['<resource>'])) elif arguments['file']: output(vtc.rscReport(arguments['<resource>'])) elif arguments['--rescan']: arguments['scan'] = False arguments['report'] = True arguments['<resource>'] = data['scan_id'] key = readconfig('virustotal') vtc = Virustotal(key) output(vtc.rscReport(arguments['<resource>']))
output_df: DataFrame = df.groupby(["id", "name", "user_email"]) \ .agg({"duration": ["sum"], "join_time": ["min"], "leave_time": ["max"]}) \ .reset_index() \ .rename(columns={"duration": "total_duration"}) output_df.columns = output_df.columns.get_level_values(0) output_df.total_duration = round(output_df.total_duration / 3600, 2) output_df.join_time = output_df.join_time.dt.strftime("%Y-%m-%d %H:%M:%S") output_df.leave_time = output_df.leave_time.dt.strftime( "%Y-%m-%d %H:%M:%S") meeting_date: str = output_df.join_time.tolist()[0].split(" ")[0] output_file: str = f"zoom_report_{meeting_date}" googl = Googl(SERVICE_ACCOUNT_FILE, SCOPES) zoom_folder_id: str = googl.get_folder_id("Zoom") sheet_id = googl.create_new_sheet(output_file, zoom_folder_id) result = googl.insert_df_to_sheet(sheet_id, output_df) sheet_link = googl.get_sheet_link(result.get("spreadsheetId")) print(f"Finished uploading Zoom report.\n" f"spreadsheetId: {result.get('updates').get('spreadsheetId')}\n" f"updatedRange: {result.get('updates').get('updatedRange')}\n" f"updatedRows: {result.get('updates').get('updatedRows')}\n" f"link: {sheet_link}")
def table(data): try: googlkey = readconfig('googl') g = Googl(googlkey) shorten = True except: googlkey = None shorten = False metatable = PrettyTable() metafields = collections.OrderedDict() if arguments['report']: if not arguments['ip'] and not arguments['domain']: if arguments['url']: metafields['URL'] = data['url'] elif arguments['file'] or arguments['hash']: metafields['MD5'] = data['md5'] metafields['SHA1'] = data['sha1'] metafields['SHA256'] = data['sha256'] if int(data['positives']) > (int(data['total']) / 2): c = red else: c = green detectionratio = '{0}/{1}'.format(data['positives'], data['total']) metafields['Detection ratio'] = '{0}'.format(detectionratio) metafields['Analysis date'] = data['scan_date'] metafields['Scan id'] = data['scan_id'] if shorten: link = g.shorten(data['permalink'])['id'] else: link = data['permalink'] metafields['Link'] = link for f in metafields: col = green if f == 'Detection ratio': col = c metatable.add_row([colorize(colorize(f, blue), bold), colorize(str(metafields[f]), col)]) metatable.align = "l" metatable.header = False print metatable scans = data['scans'] scanstable = PrettyTable(colorize(colorize(['Engine', 'Detected', 'Result', 'Detail'], blue), bold)) for key in scans.keys(): engine = key detected = scans[key]['detected'] result = scans[key]['result'] if 'detail' in scans[key]: if shorten: detail = g.shorten(scans[key]['detail'])['id'] else: detail = scans[key]['detail'] else: detail = None if detected: scanstable.add_row(colorize([engine, detected, result, detail], red)) else: scanstable.add_row(colorize([engine, detected, result, detail], green)) scanstable.align = "l" print scanstable elif arguments['ip'] or arguments['domain']: if arguments['ip']: headtype = 'Hostname' headtype2 = 'hostname' if 'asn' in data: metafields['AS owner'] = data['as_owner'] metafields['ASN'] = data['asn'] metafields['Country'] = data['country'] for f in metafields: metatable.add_row([colorize(colorize(f, blue), bold), colorize(str(metafields[f]), green)]) metatable.align = "l" metatable.header = False print metatable elif arguments['domain']: headtype = 'IP address' headtype2 = 'ip_address' cattable = PrettyTable(colorize(colorize(['Categories'], blue), bold)) for c in data['categories']: cattable.add_row([colorize(str(c), green)]) cattable.align = "l" print cattable if 'WOT domain info' in data: print 'WOT domain info' w = PrettyTable() for k in data['WOT domain info']: w.add_row([colorize(colorize(str(k), blue), bold), colorize(str(data['WOT domain info'][k]), green)]) w.align = "l" w.header = False print w if 'subdomains' in data: subtable = PrettyTable(colorize(colorize(['Subdomains'], blue), bold)) for s in data['subdomains']: subtable.add_row([colorize(str(s), green)]) subtable.align = "l" print subtable whoistable = PrettyTable(colorize(colorize(['Whois lookup'], blue), bold)) whoistable.add_row([data['whois']]) whoistable.align = "l" print whoistable if len(data['resolutions']) > 0: print 'Resolutions {0}'.format(len(data['resolutions'])) restable = PrettyTable(colorize(colorize([headtype, 'Last resolved'], blue), bold)) for ip in data['resolutions']: restable.add_row(colorize([ip[headtype2], ip['last_resolved']], green)) restable.align = "l" print restable if len(data['detected_urls']) > 0: print 'URLs {0}'.format(len(data['detected_urls'])) urltable = PrettyTable(colorize(colorize(['Analysis date', 'Detection ratio', 'URL'], blue), bold)) for u in data['detected_urls']: adate = u['scan_date'] positives = u['positives'] total = u['total'] url = u['url'] ratio = '{0}/{1}'.format(positives, total) if int(positives) > (int(total) / 2): c = red else: c = green urltable.add_row(colorize([adate, ratio, url], c)) urltable.align = "l" print urltable if 'detected_referrer_samples' in data: print 'Detected referrer samples {0}'.format( len(data['detected_referrer_samples'])) dreftable = PrettyTable(colorize(colorize(['SHA256', 'Detection ratio'], blue), bold)) for dref in data['detected_referrer_samples']: positives = dref['positives'] total = dref['total'] ratio = '{0}/{1}'.format(positives, total) shahash = dref['sha256'] if int(positives) > (int(total) / 2): c = red else: c = green dreftable.add_row(colorize([shahash, ratio], c)) dreftable.align = "l" print dreftable if 'detected_downloaded_samples' in data: print 'Detected downloaded samples {0}'.format( len(data['detected_downloaded_samples'])) ddowntable = PrettyTable(colorize(colorize(['Analysis date', 'SHA256', 'Detection ratio'], blue), bold)) for ddown in data['detected_downloaded_samples']: adate = ddown['date'] positives = ddown['positives'] total = ddown['total'] ratio = '{0}/{1}'.format(positives, total) shahash = ddown['sha256'] if int(positives) > (int(total) / 2): c = red else: c = green ddowntable.add_row(colorize([adate, shahash, ratio], c)) ddowntable.align = "l" print ddowntable if 'detected_communicating_samples' in data: print 'Detected communicating samples {0}'.format( len(data['detected_communicating_samples'])) dcommtable = PrettyTable(colorize(colorize(['Analysis date', 'SHA256', 'Detection ratio'], blue), bold)) for dcomm in data['detected_communicating_samples']: adate = dcomm['date'] positives = dcomm['positives'] total = dcomm['total'] ratio = '{0}/{1}'.format(positives, total) shahash = dcomm['sha256'] if int(positives) > (int(total) / 2): c = red else: c = green dcommtable.add_row(colorize([adate, shahash, ratio], c)) dcommtable.align = "l" print dcommtable elif arguments['scan'] and not arguments['--rescan']: if arguments['url']: metafields['URL'] = data['url'] elif arguments['file']: metafields['MD5'] = data['md5'] metafields['SHA1'] = data['sha1'] metafields['SHA256'] = data['sha256'] metafields['Scan id'] = data['scan_id'] if shorten: link = g.shorten(data['permalink'])['id'] else: link = data['permalink'] metafields['Link'] = link for f in metafields: metatable.add_row([colorize(colorize(f, blue), bold), colorize(str(metafields[f]), green)]) metatable.align = "l" metatable.header = False print metatable time.sleep(30) arguments['scan'] = False arguments['report'] = True arguments['<resource>'] = data['scan_id'] key = readconfig('virustotal') vtc = Virustotal(key) if arguments['url']: output(vtc.urlReport(arguments['<resource>'])) elif arguments['file']: output(vtc.rscReport(arguments['<resource>'])) elif arguments['--rescan']: arguments['scan'] = False arguments['report'] = True arguments['<resource>'] = data['scan_id'] key = readconfig('virustotal') vtc = Virustotal(key) output(vtc.rscReport(arguments['<resource>']))