Last active
March 29, 2018 03:25
-
-
Save KhasMek/5a22cf870d24154a310b8235802d5f92 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2 -u | |
# | |
# REQUIREMENTS: | |
# - sre_yield | |
# | |
# AUTHOR: | |
# - KhasMek | |
# | |
import datetime | |
import json | |
import re | |
import requests | |
import sre_yield | |
import time | |
api_key = '' | |
characters = '[A-Za-z0-9]' | |
min_length = 1 | |
max_length = 3 | |
regular_expression = r'{char}{{{min},{max}}}'.format(char=characters, min=min_length, max=max_length) | |
base_url = 'https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo.gl/' | |
fields = 'id,longUrl,status,analytics(allTime)' | |
already_checked_file = 'already-checked.txt' | |
results_file = 'results.json' | |
delay = 5 | |
def get_response(short_url_tag): | |
if fields: | |
full_url = str('{base_url}{short_url_tag}&projection=FULL&key={key}&fields={fields}'.format(base_url=base_url, short_url_tag=short_url_tag, key=api_key, fields=fields)) | |
else: | |
full_url = str('{base_url}{short_url_tag}&projection=FULL&key={key}'.format(base_url=base_url, short_url_tag=short_url_tag, key=api_key)) | |
r = requests.get(full_url) | |
return json.loads(r.text) | |
def main(): | |
now = {"start": str(datetime.datetime.now())} | |
with open(results_file, 'a+') as results: | |
json.dump(now, results) | |
results.write('\n') | |
for short_url_tag in sre_yield.AllMatches(regular_expression): | |
checked = False | |
with open(already_checked_file, 'a+') as file: | |
tag = re.compile("^{}$".format(short_url_tag), re.MULTILINE) | |
# I should break this into its own function and just do a check? | |
if tag.search(file.read()): | |
print(" [!] ALREADY CHECKED:\t{}".format(short_url_tag)) | |
checked = True | |
if not checked: | |
print(" [*] NOW CHECKING:\t{}".format(short_url_tag)) | |
response = get_response(short_url_tag) | |
if 'error' in response and response['error']['code'] == 403: | |
print(' [!] RATE LIMIT:\tSLEEPING FOR 300 SECONDS') | |
time.sleep(300) | |
response = get_response(short_url_tag) | |
if 'error' in response: # and response['error']['code'] != 403: | |
print(' [!] ERROR:\t\t{}\t{}\t{}'.format(short_url_tag, response['error']['code'], response['error']['message'])) | |
else: | |
print(' [+] SHORT URL FOUND:\t{}\t{}'.format(short_url_tag, response['longUrl'])) | |
with open(results_file, 'a+') as results: | |
item = {short_url_tag: response} | |
json.dump(item, results) | |
results.write('\n') | |
with open(already_checked_file, 'a+') as ac: | |
ac.write(short_url_tag + "\n") | |
time.sleep(delay) | |
with open(results_file, 'a+') as results: | |
now = {"end": str(datetime.datetime.now())} | |
json.dump(now, results) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment