From a67113305c5c235e367f86c3145144074027d351 Mon Sep 17 00:00:00 2001 From: andreimarcu Date: Sun, 6 Apr 2014 19:35:14 -0400 Subject: [PATCH] Added .posted (local version) --- modules/head.py | 15 ++++++++-- modules/posted.py | 71 +++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 3 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 modules/posted.py diff --git a/modules/head.py b/modules/head.py index c68d834..c35b2ed 100644 --- a/modules/head.py +++ b/modules/head.py @@ -12,6 +12,7 @@ import urllib.parse import time from html.entities import name2codepoint import web +from modules.posted import check_posted from tools import deprecated @@ -89,7 +90,7 @@ noteuri.priority = 'low' def snarfuri(phenny, input): uri = input.group(1) - title = gettitle(phenny, uri) + title = gettitle(phenny, input, uri) if title: phenny.msg(input.sender, title) @@ -98,7 +99,7 @@ snarfuri.priority = 'low' snarfuri.thread = True -def gettitle(phenny, uri): +def gettitle(phenny, input, uri): if not ':' in uri: uri = 'http://' + uri uri = uri.replace('#!', '?_escaped_fragment_=') @@ -179,10 +180,18 @@ def gettitle(phenny, uri): title = title.replace('\n', '') title = title.replace('\r', '') title = "[ {0} ]".format(title) + + if "posted" in phenny.variables: + posted = check_posted(phenny, input, uri) + + if posted: + title = "{0} (posted: {1})".format(title, posted) + + else: title = None return title if __name__ == '__main__': - print(__doc__.strip()) + print(__doc__.strip()) \ No newline at end of file diff --git a/modules/posted.py b/modules/posted.py new file mode 100644 index 0000000..bba5e0f --- /dev/null +++ b/modules/posted.py @@ -0,0 +1,71 @@ +#!/usr/bin/python3 +""" +posted.py - Remembers who posted which URL, can show on URL match. +author: andreim +""" +import os +import sqlite3 +from ago import human + + +def setup(self): + fn = self.nick + '-' + self.config.host + '.posted.db' + self.posted_db = os.path.join(os.path.expanduser('~/.phenny'), fn) + conn = sqlite3.connect(self.posted_db) + + c = conn.cursor() + c.execute('''create table if not exists posted ( + channel varchar(255), + nick varchar(255), + url varchar(512), + time timestamp date default (datetime('now', 'localtime')) + );''') + + c.close() + conn.close() + + +def check_posted(phenny, input, url): + if url: + conn = sqlite3.connect(phenny.posted_db, + detect_types=sqlite3.PARSE_DECLTYPES) + c = conn.cursor() + c.execute("SELECT nick, time FROM posted WHERE channel=? AND url=?", + (input.sender, url)) + res = c.fetchone() + + posted = None + + if res: + nickname = res[0] + time = human(res[1]) + + posted = "{0} by {1}".format(time, nickname) + + + else: + c.execute("INSERT INTO posted (channel, nick, url) VALUES (?, ?, ?)", + (input.sender, input.nick, url)) + conn.commit() + + conn.close() + + return posted + + +def posted(phenny, input): + if not input.group(2): + return phenny.say(".posted - checks if URL has been posted" + + " before in this channel.") + url = input.group(2) + + posted = check_posted(phenny, input, url) + if posted: + # when the day comes when you can inhibit snarfuri if modules are called + # phenny.reply("URL was posted {0}".format(posted)) + pass + else: + phenny.reply("I don't remember seeing this URL in this channel.") + +posted.thread = False +posted.commands = ["posted"] \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 010b492..e6d7cf3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ lxml mock nose requests +ago \ No newline at end of file