-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
159 lines (133 loc) · 5.04 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/local/bin/python3
import typer
from tinydb import TinyDB, Query
import os
from os.path import expanduser
import favicon
from bs4 import BeautifulSoup as bs
import requests
from typer import Option
from typing import Optional
import webbrowser
from fuzzywuzzy import fuzz, process
import jinja2
from tabulate import tabulate
app = typer.Typer()
home = expanduser("~")
database_file = os.path.join(home, '.database_cli.json')
db = TinyDB(database_file)
query = Query()
find_list = Query()
TEMPLATE = """<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" />
<style>
body {
font-family: sans-serif;
}
code, pre {
font-family: monospace;
}
h1 code,
h2 code,
h3 code,
h4 code,
h5 code,
h6 code {
font-size: inherit;
}
</style>
</head>
<body>
<div class="container">
<table class="table table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Title</th>
<th scope="col">URL</th>
<th scope="col">FavIcon</th>
<th scope="col">Tags</th>
</tr>
</thead>
<tbody>
{%for x in content %}
<tr>
<td>{{ x['Count'] }}</td>
<td>{{ x['Title'] }}</td>
<td><a href="{{ x['URL'] }}">Visit Site</td>
<td><img style='width: 40px;' src=' {{ x['Icon'] }}'/></td>
<td>{% for y in x['Tags'] %}
<a href="#" class="badge badge-secondary">{{ y }}</a>
{% endfor %}</td>
</td>
</tr>
{% endfor %}
</tbody>
</div>
</body>
</html>"""
@app.command(help="Returns the number of entries")
def count(count: str = None):
typer.echo(len(db.all()))
@app.command(help="Create an entry given a URL")
def add(url: str):
tags = typer.prompt("Any tags? - comma seperated list")
tags = tags.lower()
try:
icon = favicon.get(url)[0][0]
except:
icon = None
html = requests.get(url)
soup = bs(html.content, 'html.parser')
title = soup.find('title')
title = title.get_text()
db.insert({ "Title": title, "URL": url, "Icon": icon, "Tags": tags.split(",") })
typer.echo("{} saved with - Title: {} - Favicon: {}".format(typer.style(url, fg=typer.colors.GREEN, bold=False), typer.style(title, fg=typer.colors.GREEN, bold=True), typer.style(icon, fg=typer.colors.GREEN, bold=True) ))
@app.command(help="Search for an entry by title given a search term. Provide the second, optional, argument 'True' to open the returned URL in the default browser")
def find(title: str, open: Optional[bool] = typer.Argument(False)):
lookup = db.search(query.Title == title)
for item in lookup:
results = "Title: {}, URL: {}, Tags: {}".format( typer.style(item['Title'], fg=typer.colors.BRIGHT_BLUE, bold=True),
typer.style(item['URL'], fg=typer.colors.RED, bold=False), typer.style( str(item['Tags']) , fg=typer.colors.YELLOW, bold=False) )
if open != False:
webbrowser.open_new(lookup[0]['URL'])
typer.echo(results)
@app.command("tags", help="Provide a single word search term and all entries with tags that match the given term will be returned.")
def search_tags(tag: str):
lookup = db.search(query.Tags.any(tag))
typer.echo(lookup)
@app.command("list", help="Lists all entries") # This over rides the usual call - which would be "collection"
def collection():
lookup = db.all()
header = lookup[0].keys()
rows = [x.values() for x in lookup]
output = tabulate(rows, headers=header)
typer.echo(output)
@app.command("text", help="Given a string, fuzzy search the entries and return the top three results.")
def fuzzysearch(term: str):
choices = db.all()
guess = process.extract(term, choices, limit=3)
final = []
for item in guess:
results = "Entry: {} \n Percent Certain: {}".format(item[0], item[1])
final.append(results)
typer.echo(final)
@app.command("dashboard", help="Open a webpage with the entries presented")
def dashboard_page():
entries = db.all()
count = 1
list_of_rows = []
for item in entries:
if item['Icon'] == None:
item['Icon'] = "https://storage.googleapis.com/template-design/icons/svg_icons_big-list/windows/page-question.svg"
list_of_rows.append({ "Count": count, "Title": item['Title'], "URL": item['URL'], "Icon": item['Icon'], "Tags": item['Tags'] })
count += 1
doc = jinja2.Template(TEMPLATE).render(content=list_of_rows)
open(os.path.join(home, 'Dashboard.html'), 'w').write(doc)
#output.write(doc, 'w')
#webbrowser.open_new("file:///{}".format(os.path.join(home, 'Dashboard.html')))
typer.echo("Done")
if __name__ == "__main__":
app()