Last active
June 12, 2020 08:56
-
-
Save hannesdatta/2c36168542ca77d73853f40097136450 to your computer and use it in GitHub Desktop.
Bulk-transform URLs to short(ened) URLs via tinyurl
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
# TRANSFORMS A SET OF URLS TO TINY-URLS (SHORTENED URLS) | |
# adapted from https://www.geeksforgeeks.org/python-url-shortener-using-tinyurl-api/ |
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
# TRANSFORMS A SET OF URLS TO TINY-URLS (SHORTENED URLS) | |
# adapted from https://www.geeksforgeeks.org/python-url-shortener-using-tinyurl-api/ | |
# Input: urls.txt with one URL per line | |
# Output: tiny_urls.txt, with the original URL and the shortened one | |
from __future__ import with_statement | |
import contextlib | |
try: | |
from urllib.parse import urlencode | |
except ImportError: | |
from urllib import urlencode | |
try: | |
from urllib.request import urlopen | |
except ImportError: | |
from urllib2 import urlopen | |
#import sys | |
import time | |
def make_tiny(url): | |
request_url = ('http://tinyurl.com/api-create.php?' + urlencode({'url':url})) | |
with contextlib.closing(urlopen(request_url)) as response: | |
return response.read().decode('utf-8 ') | |
con = open('urls.txt', 'r',encoding='latin1').readlines() | |
urls = [] | |
for i in con: urls.append(i.replace('\n', '').replace('\t','')) | |
f=open('tiny_urls.txt','w',encoding='utf-8') | |
for i in urls: | |
tiny='NA' | |
print(i) | |
if (len(i)>3): tiny=make_tiny(i) | |
f.write(i+'\t'+tiny+'\n') | |
time.sleep(.5) | |
f.close() |
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
http://tilburgsciencehub.com http://tinyurl.com/yagoooq4 | |
http://hannesdatta.com http://tinyurl.com/yanuuq9x | |
https://thesis.hannesdatta.com http://tinyurl.com/ya6qyp8k | |
NA |
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
http://tilburgsciencehub.com | |
http://hannesdatta.com | |
https://thesis.hannesdatta.com | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment