Created
April 9, 2023 20:38
-
-
Save mattsdni/5757c2b26375ea00c42ff2fd9dcf44c5 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
import requests | |
long_url = '' # TODO put your original link here | |
# List of custom aliases for each short URL | |
aliases = ['djkfhsfkljewh', 'we89hidsuhvkshd', 'sdhfewkluhdskvjhsd'] # TODO update your aliases here | |
api_key = '' # TODO add your api key here | |
# Create headers with your API key | |
headers = { | |
'X-API-KEY': api_key | |
} | |
# Create short URLs for each alias using TinyURL API | |
tinyurls = [] | |
if __name__ == '__main__': | |
tinyurls = [] | |
# Create short URLs for each alias using TinyURL API | |
for alias in aliases: | |
params = { | |
'url': long_url, | |
'alias': alias | |
} | |
r = requests.post(f'https://api.tinyurl.com/create?api_token={api_key}', data=params, headers=headers) | |
if r.status_code == 200: | |
tinyurl = r.json()['data']['tiny_url'] | |
expected = f'https://tinyurl.com/{alias}' | |
if tinyurl != expected: | |
print(f'alias already taken. wanted {expected} but got {tinyurl}') | |
else: | |
print(f'Error creating short URL for alias "{alias}"') | |
tinyurl = None | |
tinyurls.append(tinyurl) | |
print(tinyurls) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment