This repository has been archived by the owner on Jun 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
url-logger.py
53 lines (41 loc) · 2.05 KB
/
url-logger.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
from datetime import datetime
import hexchat
import os
import re
__module_name__ = "URL Logger"
__module_author__ = "Poorchop"
__module_version__ = "0.1"
__module_description__ = "Log URLs from specific channels and PMs to disk"
# channels PMs
watched_channels = ("#hexchat", "TingPing")
events = ("Channel Message", "Channel Action",
"Channel Msg Hilight", "Channel Action Hilight",
"Private Message", "Private Message to Dialog",
"Private Action", "Private Action to Dialog")
# regex source: http://blog.mattheworiordan.com/post/13174566389/url-regular-expression-for-links-with-or-without-the
url_regex = re.compile("((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A"
"-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)")
def url_logger(stripped_word, nick, network, chan, time):
directory = os.path.join(hexchat.get_info("configdir"), "logs", network, chan)
if not os.path.exists(directory):
os.makedirs(directory)
directory = os.path.join(hexchat.get_info("configdir"), "logs", network, chan, "urls.txt")
f = open(directory, "a")
f.write(time + " " + nick + "@" + chan + ":" + network + " - " + stripped_word + "\n")
f.close()
def url_finder(word, nick, network, chan, time):
for w in word[1].split():
stripped_word = hexchat.strip(w, -1, 3)
if url_regex.match(stripped_word):
url_logger(stripped_word, nick, network, chan, time)
def chan_check_cb(word, word_eol, userdata):
word = [(word[i] if len(word) > i else "") for i in range(4)]
chan = hexchat.get_info("channel")
if chan in watched_channels:
nick = hexchat.strip(word[0], -1, 3)
time = datetime.now().strftime("[%b %d %Y %H:%M]")
network = hexchat.get_info("network")
url_finder(word, nick, network, chan, time)
for event in events:
hexchat.hook_print(event, chan_check_cb)
hexchat.prnt(__module_name__ + " version " + __module_version__ + " loaded")