-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathirclog-cli.py
More file actions
executable file
·188 lines (150 loc) · 4.85 KB
/
irclog-cli.py
File metadata and controls
executable file
·188 lines (150 loc) · 4.85 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#! /usr/bin/python
from requests.compat import urljoin
import requests
from datetime import datetime
import os
import json
import pprint
global VERBOSE
IRCLOG_URL = "https://db.softver.org.mk/irclog/"
def print_message(doc):
if VERBOSE:
pprint.pprint(doc)
return
t = datetime.fromtimestamp(doc["timestamp"])
tm = t.strftime("%H:%M:%S")
dt = t.strftime("%Y-%m-%d")
sender = doc["sender"]
msg = doc.get("message", doc.get("notice"))
print(f"{dt} {tm} {sender}: {msg}")
def get_backlog(channel, limit=100):
url = urljoin(IRCLOG_URL, "_design/log/_view/channel")
startkey = [channel, {}]
endkey = [channel, 0]
query = dict(
update_seq="true",
reduce="false",
descending="true",
limit=limit,
include_docs="true",
startkey=startkey,
endkey=endkey,
)
req = requests.post(url, json=query)
if not req.ok:
req.raise_for_status()
backlog = req.json()
update_seq = backlog["update_seq"]
def _gen():
for row in sorted(backlog["rows"], key=lambda r: r["doc"]["timestamp"]):
yield row["doc"]
return update_seq, _gen()
def get_changes(channel, since=None, feed="continuous"):
url = urljoin(IRCLOG_URL, "_changes")
query = dict(
feed=feed,
filter="_selector",
heartbeat=30000,
include_docs="true",
since=since,
)
data = {"selector": {"channel": channel}}
req = requests.post(url, params=query, json=data, stream=True, timeout=60)
if not req.ok:
req.raise_for_status()
return req
def filter_changes(req):
for row in req.iter_lines(chunk_size=None, decode_unicode=True):
if row.strip():
change = json.loads(row)
doc = change["doc"]
yield change["seq"], doc
def list_channels(_args):
url = urljoin(IRCLOG_URL, "_design/log/_view/channel")
query = {"group_level": 1}
req = requests.post(url, json=query)
if not req.ok:
req.raise_for_status()
for ch in req.json()["rows"]:
print(ch["key"][0], ch["value"])
def search(args):
needle = " ".join(args.needle)
url = urljoin(IRCLOG_URL, "_find?include_docs=true")
q = {
"selector": {
"channel": args.channel,
"message": {"$regex": needle},
# "timestamp": { "$gt" или "$lt": 1537798438 }
},
"fields": ["_id", "timestamp", "sender", "message", "channel"],
}
req = requests.post(url, json=q)
if not req.ok:
req.raise_for_status()
for doc in req.json()["docs"]:
print_message(doc)
def follow(args):
update_seq, msgs = get_backlog(args.channel, limit=args.limit)
for msg in msgs:
print_message(msg)
follow_loop(args.channel, update_seq)
def follow_loop(channel, update_seq):
done = False
while not done:
try:
req = get_changes(channel, since=update_seq)
for seq, msg in filter_changes(req):
print_message(msg)
update_seq = seq
except KeyboardInterrupt:
print()
done = True
except requests.exceptions.ConnectionError:
pass
def open_dump_file(filename):
try:
fp = open(args.file, "r")
obj = json.load(fp)
return obj
except:
return {"results": [], "last_seq": None}
def dump(args):
obj = open_dump_file(args.file)
since = obj["last_seq"]
req = get_changes(args.channel, since=since, feed="normal")
j = req.json()
obj["last_seq"] = j["last_seq"]
obj["results"].extend(j["results"])
updated = args.file + '~'
fp = open(updated, 'x')
json.dump(obj, fp)
os.replace(updated, args.file)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--verbose", "-v", action="count")
subparsers = parser.add_subparsers(title="subcommands")
s = subparsers.add_parser("search", help="search <channel> <text…>")
s.add_argument("channel")
s.add_argument("needle", metavar="text", nargs="+", help="text to search for")
s.set_defaults(func=search)
f = subparsers.add_parser(
"follow", help="follow <channel> [backlog limit, default 100]"
)
f.add_argument("channel", help="channel to look at")
f.add_argument("limit", default=100, nargs="?", type=int, help="limit backlog")
f.set_defaults(func=follow)
d = subparsers.add_parser(
"dump", help="dump <channel> <file>"
)
d.add_argument("channel", help="channel to dump")
d.add_argument("file", help="dump file in json")
d.set_defaults(func=dump)
l = subparsers.add_parser("list", help="list all channels")
l.set_defaults(func=list_channels)
args = parser.parse_args()
VERBOSE = args.verbose
if "func" in args:
args.func(args)
else:
parser.print_help()