Commit 431c56c3 authored by Mindiell's avatar Mindiell

Added better logging

parent b7cf2e93
...@@ -28,6 +28,41 @@ import urllib ...@@ -28,6 +28,41 @@ import urllib
import config import config
from messages import messages from messages import messages
LOG_FILE = "wantzel.log"
DEBUG = 3
WARNING = 2
INFO = 1
ERROR = 0
LOG_LEVEL = DEBUG
class Utils(object):
@classmethod
def log(cls, message):
with open(LOG_FILE, 'a') as f:
f.write("%s: %s\n" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), message))
@classmethod
def debug(cls, message):
if LOG_LEVEL>=DEBUG:
cls.log("%s: %s" % ("DEBUG", message))
@classmethod
def warning(cls, message):
if LOG_LEVEL>=WARNING:
cls.log("%s: %s" % ("WARNING", message))
@classmethod
def info(cls, message):
if LOG_LEVEL>=INFO:
cls.log("%s: %s" % ("INFO", message))
@classmethod
def error(cls, message):
if LOG_LEVEL>=ERROR:
cls.log("%s: %s" % ("ERROR", message))
def get_cursor(): def get_cursor():
""" """
This function connects to a MySQL database and returns a usable cursor. This function connects to a MySQL database and returns a usable cursor.
...@@ -121,7 +156,7 @@ class Wantzel(object): ...@@ -121,7 +156,7 @@ class Wantzel(object):
""" """
This method launches function regularly (see config.timer). This method launches function regularly (see config.timer).
""" """
print("Timer called") Utils.debug("Timer called")
self.rp_to_twitter("http://www.laquadrature.net/fr/revue-de-presse/feed") self.rp_to_twitter("http://www.laquadrature.net/fr/revue-de-presse/feed")
self.rp_to_twitter("http://www.laquadrature.net/en/press-review/feed") self.rp_to_twitter("http://www.laquadrature.net/en/press-review/feed")
self.count_articles() self.count_articles()
...@@ -164,7 +199,7 @@ class Wantzel(object): ...@@ -164,7 +199,7 @@ class Wantzel(object):
""" """
# Cleaning user name # Cleaning user name
user = re.search("([^!]*)!", user).group(1) user = re.search("([^!]*)!", user).group(1)
#print("Message received: %s %s %s" % (user, channel, msg)) Utils.debug("Message received: %s %s %s" % (user, channel, msg))
# Whatever is done, get the title of an existing url in a message # Whatever is done, get the title of an existing url in a message
#title = "" #title = ""
#if "http" in msg: #if "http" in msg:
...@@ -181,7 +216,7 @@ class Wantzel(object): ...@@ -181,7 +216,7 @@ class Wantzel(object):
command = re.search("!(rp[acp]*|kill|help|stats|admin)", msg) command = re.search("!(rp[acp]*|kill|help|stats|admin)", msg)
if command: if command:
command = command.group(1) command = command.group(1)
print("Command: %s" % command) Utils.debug("Command: %s" % command)
if command.startswith("rp"): if command.startswith("rp"):
self.rp(command, user, channel, msg, title) self.rp(command, user, channel, msg, title)
elif command=="help": elif command=="help":
...@@ -202,7 +237,7 @@ class Wantzel(object): ...@@ -202,7 +237,7 @@ class Wantzel(object):
If a known command is behind the !help command, an adequate message is If a known command is behind the !help command, an adequate message is
returned. returned.
""" """
print("help command") Utils.debug("help command")
# Searching for a command after help keyword # Searching for a command after help keyword
command = re.search("!help (stats|rp|help|kill|admin)", msg) command = re.search("!help (stats|rp|help|kill|admin)", msg)
if command: if command:
...@@ -215,11 +250,11 @@ class Wantzel(object): ...@@ -215,11 +250,11 @@ class Wantzel(object):
""" """
Adding the article in rp database. Adding the article in rp database.
""" """
print("rp command %s" % command) Utils.debug("rp command %s" % command)
cite = 0 cite = 0
note = 0 note = 0
url = get_url(msg) url = get_url(msg)
print("url: %s" % url) Utils.debug("url: %s" % url)
if url=="": if url=="":
return return
elif url=="http": elif url=="http":
...@@ -240,7 +275,7 @@ class Wantzel(object): ...@@ -240,7 +275,7 @@ class Wantzel(object):
# Archive this article # Archive this article
if "a" in command: if "a" in command:
note -= 2 note -= 2
print("Adding an article by %s: %s" % (user, url)) Utils.debug("Adding an article by %s: %s" % (user, url))
result = cursor.execute( result = cursor.execute(
"""INSERT INTO presse SET """INSERT INTO presse SET
url=%s, provenance=%s, cite=%s, note=%s, datec=NOW(), title=%s, url=%s, provenance=%s, cite=%s, note=%s, datec=NOW(), title=%s,
...@@ -251,7 +286,7 @@ class Wantzel(object): ...@@ -251,7 +286,7 @@ class Wantzel(object):
self.send_message(channel, messages["rp_new_article"] % user) self.send_message(channel, messages["rp_new_article"] % user)
else: else:
if rows[0][2]!=user: if rows[0][2]!=user:
print("Adding a point by %s on %s" % (user, rows[0][0])) Utils.debug("Adding a point by %s on %s" % (user, rows[0][0]))
result = cursor.execute( result = cursor.execute(
"UPDATE presse SET note=note+1 WHERE id=%s", "UPDATE presse SET note=note+1 WHERE id=%s",
(rows[0][0], ) (rows[0][0], )
...@@ -267,10 +302,10 @@ class Wantzel(object): ...@@ -267,10 +302,10 @@ class Wantzel(object):
""" """
Kill an article by setting its score to -100. Kill an article by setting its score to -100.
""" """
print("kill command") Utils.debug("kill command")
if is_moderator(user): if is_moderator(user):
url = get_url(msg) url = get_url(msg)
print("url: %s" % url) Utils.debug("url: %s" % url)
if url=="": if url=="":
return return
elif url=="http": elif url=="http":
...@@ -292,7 +327,7 @@ class Wantzel(object): ...@@ -292,7 +327,7 @@ class Wantzel(object):
""" """
Returns stats on articles in press review. Returns stats on articles in press review.
""" """
print("stats command") Utils.debug("stats command")
cursor = get_cursor() cursor = get_cursor()
periods = [1, 3, 7, 15] periods = [1, 3, 7, 15]
notes = [0, 3 ,4] notes = [0, 3 ,4]
...@@ -326,7 +361,7 @@ class Wantzel(object): ...@@ -326,7 +361,7 @@ class Wantzel(object):
Manage moderation. Manage moderation.
A sub-command should be behind the !admin command. A sub-command should be behind the !admin command.
""" """
print("admin command") Utils.debug("admin command")
# Searching for a command after admin keyword # Searching for a command after admin keyword
command = re.search("!admin (list|add|del)", msg) command = re.search("!admin (list|add|del)", msg)
if command: if command:
...@@ -342,7 +377,7 @@ class Wantzel(object): ...@@ -342,7 +377,7 @@ class Wantzel(object):
""" """
List actual moderators. List actual moderators.
""" """
print("admin_list command") Utils.debug("admin_list command")
if is_moderator(user): if is_moderator(user):
connection = sqlite3.connect(config.sqlite_db) connection = sqlite3.connect(config.sqlite_db)
names = [] names = []
...@@ -356,7 +391,7 @@ class Wantzel(object): ...@@ -356,7 +391,7 @@ class Wantzel(object):
""" """
Add some new moderators if not existing yet. Add some new moderators if not existing yet.
""" """
print("admin_add command") Utils.debug("admin_add command")
if is_moderator(user): if is_moderator(user):
try: try:
names = [] names = []
...@@ -387,7 +422,7 @@ class Wantzel(object): ...@@ -387,7 +422,7 @@ class Wantzel(object):
""" """
Delete a moderator from list. Delete a moderator from list.
""" """
print("admin_del command") Utils.debug("admin_del command")
if is_moderator(user): if is_moderator(user):
try: try:
names = [] names = []
...@@ -396,7 +431,7 @@ class Wantzel(object): ...@@ -396,7 +431,7 @@ class Wantzel(object):
names = [name for name in result.group(1).split(", ") if name!=""] names = [name for name in result.group(1).split(", ") if name!=""]
names.append(result.group(3)) names.append(result.group(3))
names = set(names) names = set(names)
#print(names) Utils.debug(names)
connection = sqlite3.connect(config.sqlite_db) connection = sqlite3.connect(config.sqlite_db)
for name in names: for name in names:
connection.execute("DELETE FROM moderator WHERE name=?", (name, )) connection.execute("DELETE FROM moderator WHERE name=?", (name, ))
...@@ -412,7 +447,7 @@ class Wantzel(object): ...@@ -412,7 +447,7 @@ class Wantzel(object):
Count number of articles not done in RP and updates the topic of the Count number of articles not done in RP and updates the topic of the
press review channel if needed. press review channel if needed.
""" """
print("count_articles method") Utils.debug("count_articles method")
cursor = get_cursor() cursor = get_cursor()
cursor.execute("""SELECT COUNT(*) FROM presse cursor.execute("""SELECT COUNT(*) FROM presse
WHERE DATE_SUB(NOW(), INTERVAL 2 MONTH)<datec WHERE DATE_SUB(NOW(), INTERVAL 2 MONTH)<datec
...@@ -429,7 +464,7 @@ class Wantzel(object): ...@@ -429,7 +464,7 @@ class Wantzel(object):
""" """
By parsing the RSS feed of the press-review, we know what to tweet. By parsing the RSS feed of the press-review, we know what to tweet.
""" """
print("rp_to_twitter method") Utils.debug("rp_to_twitter method")
now = time.localtime() now = time.localtime()
today = time.strptime("%s-%s-%s %s" % ( today = time.strptime("%s-%s-%s %s" % (
now.tm_year, now.tm_year,
...@@ -453,8 +488,8 @@ class Wantzel(object): ...@@ -453,8 +488,8 @@ class Wantzel(object):
entry.title.encode("utf-8"), entry.title.encode("utf-8"),
entry.link.encode("utf-8") entry.link.encode("utf-8")
)) ))
#print(entry.published_parsed) Utils.debug(entry.published_parsed)
#print(entry.title) Utils.debug(entry.title)
# Save last_entry_published # Save last_entry_published
self.last_entry_published = entry.published_parsed self.last_entry_published = entry.published_parsed
last_entry_published = time.strftime( last_entry_published = time.strftime(
...@@ -474,7 +509,7 @@ class Wantzel(object): ...@@ -474,7 +509,7 @@ class Wantzel(object):
""" """
Tweet message on specified account Tweet message on specified account
""" """
print("tweet method") Utils.debug("tweet method")
auth = OAuth( auth = OAuth(
config.TOKEN, config.TOKEN,
config.TOKENSEC, config.TOKENSEC,
...@@ -483,7 +518,7 @@ class Wantzel(object): ...@@ -483,7 +518,7 @@ class Wantzel(object):
) )
twitter = Twitter(auth=auth) twitter = Twitter(auth=auth)
try: try:
print("Tweeting: %s" % message) Utils.debug("Tweeting: %s" % message)
twitter.statuses.update(status=message) twitter.statuses.update(status=message)
except: except:
pass pass
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment