From d1051e5304dd13767bb1b00bb6725d383ac17da8 Mon Sep 17 00:00:00 2001 From: "Filip H.F. \"FiXato\" Slagter" Date: Mon, 11 Jun 2012 23:02:28 +0200 Subject: Add support for timezone offsets on a per channel basis. You can now create a file at CHANNEL_LOCATIONS_FILE that contains a list of channels and (pytz) timezone location names. All log files for specified channels from that point on will have their timestamps adjusted to the given timezone. If a channel is not specified in the config file, it will default to the timezone specified by DEFAULT_TIMEZONE (which defaults to UTC). I've started storing this info in a separate config file as I plan to move all the other settings into a config file as well so people don't need to edit the script (allowing the script to be re-used more easily). --- logbot.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/logbot.py b/logbot.py index 3e54ec8..4450926 100755 --- a/logbot.py +++ b/logbot.py @@ -38,6 +38,10 @@ import os import ftplib import sys from time import strftime +try: + from datetime import datetime + from pytz import timezone +except: pass try: from hashlib import md5 @@ -83,6 +87,8 @@ FTP_FOLDER = "" # The amount of messages to wait before uploading to the FTP server FTP_WAIT = 25 +CHANNEL_LOCATIONS_FILE = os.path.expanduser("~/.logbot-channel_locations.conf") +DEFAULT_TIMEZONE = 'UTC' default_format = { "help" : HELP_MESSAGE, @@ -167,6 +173,7 @@ class Logbot(SingleServerIRCBot): self.count = 0 self.nick_pass = nick_pass + self.load_channel_locations() print "Logbot %s" % __version__ print "Connecting to %s:%i..." % (server, port) print "Press Ctrl-C to quit" @@ -265,8 +272,14 @@ class Logbot(SingleServerIRCBot): append_line("%s/index.html" % LOG_FOLDER, '%s' % (channel.replace("#", "%23"), channel_title)) # Current log - time = strftime("%H:%M:%S") - date = strftime("%Y-%m-%d") + try: + localtime = datetime.now(timezone(self.channel_locations.get(channel,DEFAULT_TIMEZONE))) + time = localtime.strftime("%H:%M:%S") + date = localtime.strftime("%Y-%m-%d") + except: + time = strftime("%H:%M:%S") + date = strftime("%Y-%m-%d") + log_path = "%s/%s/%s.html" % (LOG_FOLDER, channel, date) # Create the log date index if it doesnt exist @@ -364,6 +377,16 @@ class Logbot(SingleServerIRCBot): def on_topic(self, c, e): self.write_event("topic", e) + # Loads the channel - timezone-location pairs from the CHANNEL_LOCATIONS_FILE + # Each line is expected to have the channelname and (pytz) TimeZone location separated by a space + # Example: + # #excid3 UTC + # #Netherlands Europe/Amsterdam + def load_channel_locations(self): + self.channel_locations = {} + if os.path.exists(CHANNEL_LOCATIONS_FILE): + f = open(CHANNEL_LOCATIONS_FILE, 'r') + self.channel_locations = dict([line.split() for line in f.readlines()]) def connect_ftp(): print "Using FTP %s..." % (FTP_SERVER) -- cgit v1.2.3-70-g09d2 From 48048ea28f862e766a1efc2ce773e903d88e0a63 Mon Sep 17 00:00:00 2001 From: "Filip H.F. \"FiXato\" Slagter" Date: Tue, 12 Jun 2012 01:37:44 +0200 Subject: Added documentation for localised channels. Includes an example of what to put in the CHANNEL_LOCATIONS_FILE config file. --- README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/README.md b/README.md index d7582f2..f18b780 100644 --- a/README.md +++ b/README.md @@ -11,3 +11,24 @@ Configuration is done inside logbot.py. python logbot.py Example logs at https://raw.github.com/excid3/logbot/master/example_logs/2011-10-22.html + + +Channels with localised time +----- +LogBot now also supports having localised time on a per channel basis using the pytz library, allowing you to have the logs of specific channels use different timezones. +Installing pytz should be as simple as running `easy_install --upgrade pytz`, see http://pytz.sourceforge.net/#installation for details. +Don't worry, if you don't have pytz installed, LogBot will continue to show the logs timestamped with your system's localtime as it used to. + +Next you can create a simple text file at `~/.logbot-channel_locations.conf` (or wherever `CHANNEL_LOCATIONS_FILE` in logbot.py points to). +On each line you can now specify a channel name, and the timezone location of which it should use the timezone offset, separated by a space. Example: + + #excid3 America/Chicago + #netherlands Europe/Amsterdam + #aloha US/Hawaii + #space UTC + +Any channel not specified in this file will use the default timezone as specified in DEFAULT_TIMEZONE, which defaults to 'UTC'. + +If you want to see a list of all possible timezone location names you can use, run: + + python -c 'import pytz;print pytz.all_timezones' -- cgit v1.2.3-70-g09d2 From 7af5c60e2afbf9df4fff8f8dcc9478901bab6462 Mon Sep 17 00:00:00 2001 From: "Filip H.F. \"FiXato\" Slagter" Date: Tue, 12 Jun 2012 01:53:04 +0200 Subject: Force lowercase channel keys Force the channel keys of the CHANNEL_LOCATIONS_FILE config to lowercase. --- logbot.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/logbot.py b/logbot.py index 4450926..0e465da 100755 --- a/logbot.py +++ b/logbot.py @@ -378,15 +378,12 @@ class Logbot(SingleServerIRCBot): self.write_event("topic", e) # Loads the channel - timezone-location pairs from the CHANNEL_LOCATIONS_FILE - # Each line is expected to have the channelname and (pytz) TimeZone location separated by a space - # Example: - # #excid3 UTC - # #Netherlands Europe/Amsterdam + # See the README for details and example def load_channel_locations(self): self.channel_locations = {} if os.path.exists(CHANNEL_LOCATIONS_FILE): f = open(CHANNEL_LOCATIONS_FILE, 'r') - self.channel_locations = dict([line.split() for line in f.readlines()]) + self.channel_locations = dict((k.lower(), v) for k, v in dict([line.split(None,1) for line in f.readlines()]).iteritems()) def connect_ftp(): print "Using FTP %s..." % (FTP_SERVER) -- cgit v1.2.3-70-g09d2 From 04aa153b0b9b150ca350ec40b044d86a60c06b30 Mon Sep 17 00:00:00 2001 From: "Filip H.F. \"FiXato\" Slagter" Date: Tue, 12 Jun 2012 02:07:07 +0200 Subject: Bugfix: forgot to strip the newlines from channellocations --- logbot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/logbot.py b/logbot.py index 0e465da..c8411d2 100755 --- a/logbot.py +++ b/logbot.py @@ -383,7 +383,7 @@ class Logbot(SingleServerIRCBot): self.channel_locations = {} if os.path.exists(CHANNEL_LOCATIONS_FILE): f = open(CHANNEL_LOCATIONS_FILE, 'r') - self.channel_locations = dict((k.lower(), v) for k, v in dict([line.split(None,1) for line in f.readlines()]).iteritems()) + self.channel_locations = dict((k.lower(), v) for k, v in dict([line.strip().split(None,1) for line in f.readlines()]).iteritems()) def connect_ftp(): print "Using FTP %s..." % (FTP_SERVER) -- cgit v1.2.3-70-g09d2