Skip to content

Instantly share code, notes, and snippets.

@mattsdni
Created April 9, 2023 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattsdni/5757c2b26375ea00c42ff2fd9dcf44c5 to your computer and use it in GitHub Desktop.
Save mattsdni/5757c2b26375ea00c42ff2fd9dcf44c5 to your computer and use it in GitHub Desktop.
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