Skip to content

Instantly share code, notes, and snippets.

@cyc115
Created April 16, 2016 03:02
Show Gist options
  • Save cyc115/f22db26de6a5d723ef6094a97f0edc6d to your computer and use it in GitHub Desktop.
Save cyc115/f22db26de6a5d723ef6094a97f0edc6d to your computer and use it in GitHub Desktop.
see what people are posting to goo.gl the url shortener service
import random
import string
import urllib.request
import time
class UrlGen :
def __init__(self, max = 1000, prefix = "" , charLen = 6):
self.max = max
self.current = 0
self.prefix = prefix
self.charLen = charLen
def __iter__(self):
return self
def __next__(self):
if self.current > self.max :
raise StopIteration
self.current += 1
return self.id_generator(self.charLen)
def id_generator(self,size =6, chars = string.ascii_uppercase + string.ascii_lowercase + string.digits ):
a = "".join(random.choice(chars) for _ in range(size))
return self.prefix.join(a)
def testUrl(url) :
try :
urllib.request.urlopen(url,timeout=400)
return 200
except urllib.error.HTTPError as e :
return e.code
except Exception :
return 401
getCurrTime = lambda : int (round(time.time() * 1000))
t1 = getCurrTime()
n = 1000 # number of urls to try
i = 0
for url in UrlGen(n,charLen=6):
url = "https://www.goo.gl/" + url
status = testUrl(url);
if status == 200 :
print( url , " works")
i+= 1
t2 = getCurrTime()
t3 = t2 - t1
print('total time spent: ' , t3)
print('per request time spent: ' , (t3 / n) )
print('number of pages found: ' , i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment