-
Notifications
You must be signed in to change notification settings - Fork 0
/
tool.py
56 lines (48 loc) · 1.62 KB
/
tool.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
import os
import datetime
import json
import click
import re
import requests
from bs4 import BeautifulSoup
@click.group(chain=True)
def cli():
pass
def validate_url(ctx, param, value):
regex = re.compile(
r'^(?:http|ftp)s?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain...
r'localhost|' # localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
if regex.match(value):
return value
else:
raise click.BadParameter('URL is not valid')
@cli.command()
@click.option('--url', callback=validate_url)
@click.option('--tags', default = None)
@click.option('--label', default = "")
@click.option('--name', default = None)
@click.option('--date', default = None)
def new_link(url, tags, label, name, date):
if tags is None:
tags = []
else:
tags = tags.split(",")
if name is None:
name = BeautifulSoup(requests.get(url).content, "html.parser").title.string
if date is None:
date = str(datetime.datetime.now()).split(".")[0]
json_filename = os.path.join('content', 'shared_links.json')
if os.path.exist(json_filename):
with open(json_filename) as data_file:
links = json.load(data_file)
else:
links = []
links.append({"date":date, "name":name, "url": url, "label": label, "tags":tags})
with open(os.path.join('content', 'shared_links.json'), 'w') as data_file:
json.dump(links, data_file)
if __name__ == '__main__':
cli()