Skip to content

Commit f33b503

Browse files
implement multi url shortener
1 parent 62906c2 commit f33b503

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

BitlyAPI/__init__.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
import requests
2+
from typing import List
3+
4+
from .model import Response
25
from .exceptions import BitlyApiNotWorking, BitlyException
36

4-
__version__ = "1.0.1"
5-
bitli_api = "https://api.ksprojects.me/bitly?url={}"
7+
__version__ = "1.0.3"
8+
bitly_api = "https://api.ksprojects.me/bitly"
69

710

8-
def shorten_url(url: str) -> str:
11+
def shorten_urls(urls: List[str]) -> List[Response]:
12+
913
try:
10-
response = requests.get(bitli_api.format(url)).json()
14+
response = requests.post(bitly_api, json={"urls": urls}).json()
1115
except Exception:
1216
raise BitlyApiNotWorking
13-
if response['success'] is True:
14-
return response.get('link')
15-
if response.get('message'):
16-
raise BitlyException(response['message'])
17+
if response.get('success') is False:
18+
raise BitlyException(response.get('message', 'error'))
19+
responses = []
20+
for d in response.get('data'):
21+
responses.append(
22+
Response('success', d.get('long_url'), d.get('short_url'))
23+
)
24+
for e in response.get('errors'):
25+
responses.append(
26+
Response(e.get('status_txt'), e['data'].get('long_url'), None)
27+
)
28+
if responses:
29+
return responses
1730
raise BitlyApiNotWorking

BitlyAPI/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ class BitlyApiNotWorking(Exception):
33

44

55
class BitlyException(Exception):
6-
pass
6+
pass

BitlyAPI/model.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Response():
2+
status_txt: str
3+
long_url: str
4+
short_url: str
5+
6+
def __init__(self, status_txt: str, long_url: str, short_url: str) -> None:
7+
self.status_txt = status_txt
8+
self.long_url = long_url
9+
self.short_url = short_url

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,22 @@ You can read our Documention on https://api.ksprojects.me/docs
2828
## <b>Short Url</b>
2929

3030
```python
31-
from BitlyAPI import shorten_url
31+
from BitlyAPI import shorten_urls
3232

33-
url = "https://github.com/Krishna-Singhal"
33+
urls = ["https://github.com/Krishna-Singhal", "...", "..."]
3434

35-
shortened_url = shorten_url(url)
36-
print(shortened_url)
35+
response = shorten_url(urls)
36+
37+
# print all shotened urls
38+
for i in response:
39+
print(i.long_url, i.short_url, i.status_txt)
3740
```
3841

3942
#### <b>Parameters</b>
4043

4144
Parameter | description
4245
--------- | -----------
43-
`url` | Long url
46+
`urls` | List of Long urls
4447

4548
<br>
4649

0 commit comments

Comments
 (0)