Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
added URL shortener
  • Loading branch information
abhinavralhan committed Oct 23, 2017
commit aa6ee3ac69c1ce377a75528d164b5ecff350bf78
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,17 @@ python client.py
python server.py
```

* ### URL Shortener
This is python script that shortens any URL provided to it.

```bash
# Takes multiple inputs and returns shortened URL for both
python shortener.py url1 url2

#Stores shortened URLs in a file
python shortener.py url1 url2 > file.txt
```

* ### Video-downloader v1.1

### About
Expand Down
24 changes: 24 additions & 0 deletions bin/shortener.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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

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')

def main():
for tinyurl in map(make_tiny, sys.argv[1:]):
print(tinyurl)

if __name__ == '__main__':
main()