-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkshower.py
More file actions
executable file
·122 lines (98 loc) · 3.97 KB
/
linkshower.py
File metadata and controls
executable file
·122 lines (98 loc) · 3.97 KB
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
#! /usr/bin/env python3
import sqlite3
import os
from flask import Flask
from flask import render_template
from flask import redirect
app = Flask(__name__)
db_path = '{}links.db'.format(os.getenv('IRC_db_path', './'))
channel = os.getenv('IRC_channel', '#linkgrabber')
server = os.getenv('IRC_server', 'irc.libera.chat')
@app.route('/')
def index():
return redirect('/1')
@app.route('/<int:page_id>')
def page(page_id):
conn = sqlite3.connect(db_path)
c = conn.cursor()
links = []
limit = str(20)
offset = str((page_id * 20) - 20)
for row in c.execute('''SELECT * FROM links ORDER BY rowid
DESC LIMIT {} OFFSET {}'''.format(limit, offset)):
links.append(row)
c.close()
next_page = page_id + 1
if page_id - 1 < 1:
previous_page = 1
else:
previous_page = page_id - 1
return render_template('links.html',
links=links,
server=server,
channel=channel,
next_page=next_page,
previous_page=previous_page)
@app.route('/<nick>')
def nick_base_page(nick):
return redirect('/{}/1'.format(nick))
@app.route('/<nick>/<int:page_id>')
def nick_page(nick, page_id):
conn = sqlite3.connect(db_path)
c = conn.cursor()
links = []
limit = str(20)
offset = str((page_id * 20) - 20)
for row in c.execute('''SELECT * FROM links WHERE nick = "{}" ORDER BY rowid
DESC LIMIT {} OFFSET {}'''.format(nick, limit, offset)):
links.append(row)
c.close()
next_page = page_id + 1
if page_id - 1 < 1:
previous_page = 1
else:
previous_page = page_id - 1
return render_template('links.html',
links=links,
server=server,
channel=channel,
nick=nick,
next_page=next_page,
previous_page=previous_page)
@app.route('/top')
def top_sites():
conn = sqlite3.connect(db_path)
c = conn.cursor()
top_sites = []
for row in c.execute('''SELECT
CASE
WHEN url LIKE 'http://%' THEN
CASE
WHEN instr(substr(url, 8), '/') > 0 THEN substr(url, 8, instr(substr(url, 8), '/') - 1)
WHEN instr(substr(url, 8), '?') > 0 THEN substr(url, 8, instr(substr(url, 8), '?') - 1)
ELSE substr(url, 8)
END
WHEN url LIKE 'https://%' THEN
CASE
WHEN instr(substr(url, 9), '/') > 0 THEN substr(url, 9, instr(substr(url, 9), '/') - 1)
WHEN instr(substr(url, 9), '?') > 0 THEN substr(url, 9, instr(substr(url, 9), '?') - 1)
ELSE substr(url, 9)
END
ELSE
CASE
WHEN instr(url, '/') > 0 THEN substr(url, 1, instr(url, '/') - 1)
WHEN instr(url, '?') > 0 THEN substr(url, 1, instr(url, '?') - 1)
ELSE url
END
END as domain,
COUNT(*) as count
FROM links
GROUP BY domain
ORDER BY count DESC
LIMIT 20'''):
top_sites.append(row)
c.close()
return render_template('top.html',
top_sites=top_sites,
server=server,
channel=channel)