-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
61 lines (48 loc) · 1.92 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import argparse
import os
from urllib.parse import urlparse, urlunparse
import requests
from dotenv import load_dotenv
def get_bitlink(long_url, headers):
url = 'https://api-ssl.bitly.com/v4/bitlinks'
payload = {'long_url': '{}'.format(long_url)}
response = requests.post(url, json=payload, headers=headers)
response.raise_for_status()
return response.json()['id']
def get_count_clicks(bitlink, headers):
bitlink = urlparse(bitlink)._replace(scheme='')
bitlink = urlunparse(bitlink)
url = 'https://api-ssl.bitly.com/v4/bitlinks/{0}/clicks/summary'.format(bitlink)
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()['total_clicks']
def is_bitlink(url, headers):
url = urlparse(url)._replace(scheme='')
url = 'https://api-ssl.bitly.com/v4/bitlinks/{0}'.format(urlunparse(url))
response = requests.get(url, headers=headers)
return response.ok
def create_url_parser():
description = 'Accepts a link as input and converts it into a bitlink.\n' \
'If a bitlink is entered, it returns the total number of clicks on it.'
parser = argparse.ArgumentParser(description=description)
parser.add_argument('urls', nargs='+', help='link address')
return parser
def main():
load_dotenv()
token = os.getenv('BITLINK_TOKEN')
headers = {'Authorization': 'Bearer {0}'.format(token)}
parser = create_url_parser()
args = parser.parse_args()
for url in args.urls:
if is_bitlink(url, headers):
try:
print(get_count_clicks(url, headers))
except requests.exceptions.HTTPError:
print('Wrong bitlink. Please, try another!')
else:
try:
print(get_bitlink(url, headers))
except requests.exceptions.HTTPError:
print('Wrong link. Please, try another!')
if __name__ == '__main__':
main()