-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1089 from nimishph/master
added url_encoder.py to Program's_Contributed_By_Contributors/PythonPrograms
- Loading branch information
Showing
2 changed files
with
37 additions
and
1 deletion.
There are no files selected for viewing
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
24 changes: 24 additions & 0 deletions
24
Program's_Contributed_By_Contributors/Python_Programs/url_encoder.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# this program uses tinyurl api to shorten given url | ||
# Contributor Name: Nimish Ph | ||
import requests | ||
from urllib.parse import urlencode | ||
|
||
from urllib3 import response | ||
|
||
def shorten(url: str) -> str: | ||
requestUrl = 'http://tinyurl.com/api-create.php?url=' + url | ||
|
||
with requests.get(requestUrl) as response: | ||
if response.status_code == 200: | ||
shortUrl = response.content.decode('UTF-8') | ||
return shortUrl | ||
else: | ||
print('Error! Status Code:', response.status_code) | ||
return None | ||
url = input('For example: https://www.wikipedia.org\nPlease enter url to shorten: ') | ||
|
||
shortUrl = shorten(url) | ||
if shortUrl is not None: | ||
print("Here's your shortened url:", shortUrl) | ||
else: | ||
print("Sorry! Couldn't shorten given url :(") |