From 0cabd705ecf613e69bff861ed139bf3514351aef Mon Sep 17 00:00:00 2001
From: vu
Date: Tue, 15 Jan 2013 17:33:23 +0100
Subject: [PATCH 1/2] boggle: added verification of answers; tell: fixed
time-bug and call-structure misunderstanding
---
modules/active/boggle.py | 115 ++++++++++++++++++++++++++++++++++--
modules/active/tell.py | 7 ++-
modules/inactive/youtube.py | 52 ++++++++++++++++
3 files changed, 167 insertions(+), 7 deletions(-)
create mode 100644 modules/inactive/youtube.py
diff --git a/modules/active/boggle.py b/modules/active/boggle.py
index 1e24c20..bc43a78 100644
--- a/modules/active/boggle.py
+++ b/modules/active/boggle.py
@@ -1,5 +1,4 @@
-import brathuhn, random, time
-
+import random, time, brathuhn
DICE = [['A','A','A','F','R', 'S']]
DICE.append(['A','A','E','E','E','E'])
DICE.append(['A','A','F','I','R','S'])
@@ -30,22 +29,130 @@ DICE.append(['I','P','R','R','R','Y'])
DICE.append(['N','O','O','T','U','W'])
DICE.append(['O','O','O','T','T','U'])
+bog_kern = [[-1,-1], [-1, 0], [-1, 1],
+ [ 0,-1] , [ 0, 1],
+ [ 1,-1], [ 1, 0], [ 1,-1]]
+
+bog_current = "asdf";
+
def timesup(a):
brathuhn.sendMsg(brathuhn.ROOM,"Time's up!",'groupchat')
+
def boggle(msg,args):
if len(args)==1:
+ global bog_current
mymsg = ""
random.shuffle(DICE)
i = 0
+
+ bog_current = [];
+ j = -1;
for d in DICE:
if i % 5 == 0:
+ bog_current.append([])
+ j = j+1
mymsg += "\n"
- i+=1
- mymsg += "%3s" % ( d[random.randint(0,5)] )
+ i+=1
+ c = d[random.randint(0,5)]
+ bog_current[j].append(c);
+ mymsg += "%3s" % ( c )
brathuhn.sendMonoMsg(brathuhn.ROOM,mymsg,'groupchat')
brathuhn.sendMsg(brathuhn.ROOM,"you have 3 minutes",'groupchat')
brathuhn.callAt(timesup, time.time()+60*3, [1])
else:
brathuhn.sendMsg(brathuhn.ROOM,"Usage: !boggle",'groupchat')
brathuhn.addCommand("boggle","Usage: !boggle",boggle)
+
+
+
+
+def bog_ans(msg, args):
+ answer = [w.strip().upper() for w in args[1:]]
+
+ valid = bog_verify(answer, bog_current)
+ invalid = list(set(answer)-set(valid))
+
+
+ count = bog_count(valid)
+ user = msg.getFrom().getResource()
+ strinv = ""
+ if len(invalid) != 0:
+ strinv = "; invalid: {0}".format(invalid)
+ brathuhn.sendMsg(brathuhn.ROOM,"{0}: {1} pts{2}".format(user, count, strinv),'groupchat')
+brathuhn.addCommand("bog_ans","Usage: !bog_ans ans1 ans2 ...",bog_ans)
+
+
+def bog_verify(msgs, bog):
+
+ ret = [];
+
+ # TODO migrate to creation of bog_current
+ bog_pad = [[""]*7]
+ for i in range(5):
+ t = [""];
+ t.extend(bog[i])
+ t.append("")
+ bog_pad.append(t)
+ bog_pad.append([""]*7)
+
+
+ for m in msgs:
+
+ # loop through bog, if first letter == m[0] then checkmachine
+ pos_r = pos_l = -1;
+
+ for r in bog:
+ pos_r += 1
+ pos_l = -1
+ for l in r:
+ pos_l += 1
+ if bog[pos_r][pos_l] == m[0]:
+
+
+ r = check_valid(m[1:], bog_pad, [[pos_r+1,pos_l+1]]);
+ if(r == 1):
+ ret.append(m)
+ return list(set(ret))
+
+
+def check_valid(m, bog, path):
+
+ # check path, no letters must be used twice
+ # pass path along, always use last position in path as
+ # pos, but check if new match exists in pos
+ c = m[0]
+ pos = path[-1:][0]
+ r = 0
+ for k in bog_kern:
+ if (bog[pos[0]+k[0]][pos[1]+k[1]] == c) and ([pos[0]+k[0],pos[1]+k[1]] not in path):
+
+ if(len(m) == 1):
+ r = 1
+ else:
+ cpath = path
+ cpath.append([pos[0]+k[0],pos[1]+k[1]])
+ r = check_valid(m[1:], bog, cpath)
+ if r == 1:
+ break
+ return r
+
+def bog_count(words):
+ count = 0;
+
+ for w in words:
+ l = len(w)
+ if l < 3:
+ continue;
+ elif l < 5:
+ count += 1
+ elif l < 6:
+ count += 2
+ elif l < 7:
+ count += 3
+ elif l < 8:
+ count += 5
+ elif l >= 8:
+ count += 11
+
+ return count
\ No newline at end of file
diff --git a/modules/active/tell.py b/modules/active/tell.py
index 4d19d0f..dfe4eaf 100644
--- a/modules/active/tell.py
+++ b/modules/active/tell.py
@@ -4,6 +4,7 @@
import brathuhn
import time
from datetime import datetime
+from datetime import timedelta
from persistent import PersistentContainer
import config
@@ -67,19 +68,19 @@ def cleartell():
now = datetime.today()
rem = []
for l in store:
- if(now - l[3] < HOLDBACK):
+ if(now - l[3] < timedelta(seconds=HOLDBACK)):
rem.append(l)
for r in rem:
store.remove(r)
container["store"] = store
- brathuhn.callAt(cleartell, time.time()+DELAY, [])
+ brathuhn.callAt(cleartell, time.time()+DELAY, [None])
##############################################
brathuhn.addCommand("tell","Usage: !tell nick text", tell)
brathuhn.addStatusListener(tellCB)
-brathuhn.callAt(cleartell, time.time()+DELAY, [])
+brathuhn.callAt(cleartell, time.time()+DELAY, [None])
##############################################
diff --git a/modules/inactive/youtube.py b/modules/inactive/youtube.py
new file mode 100644
index 0000000..cbcdb84
--- /dev/null
+++ b/modules/inactive/youtube.py
@@ -0,0 +1,52 @@
+import re, brathuhn
+import gdata.youtube.service
+
+URL = r'((\w+)://((\w+\.)+\w+)([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\/\?\.\:\;\"\,]*))'
+ID=r'[\w_-]'*11
+ID_URL=r'[=/]('+ID+r')[/]|[=/]('+ID+r')$'
+
+def get_id_from_url(url):
+ groups=re.findall(ID_URL,url)
+ if groups:
+ for g in groups:
+ id=g[0]
+ if id=='':
+ id=g[1]
+ return id
+
+def youtube_logger(mfrom,msg):
+ if brathuhn.room_nick in unicode(mfrom):
+ return
+ groups=re.findall(URL,unicode(msg.getBody()))
+
+ tmp = {}
+ for g in groups:
+ url = g[0]
+ addr = g[2]
+ protocol = g[1]
+ file = g[4]
+ if protocol in ['http','https'] and addr in ['youtube.com','www.youtube.com','youtu.be']:
+ yt_service = gdata.youtube.service.YouTubeService()
+ id = get_id_from_url(url)
+ # prevent doubles
+ if id in tmp:
+ continue
+ tmp[id]=True
+ print "youtube link",id
+ entry = yt_service.GetYouTubeVideoEntry(video_id=id)
+ sec = int(entry.media.duration.seconds)
+ duration = "%02d:%02d:%02d" % (sec/3600, (sec%3600)/60, (sec%3600)%60)
+ txt = "%s posted a youtube video: %s\nTitle: %s\nCategory: %s\nTags: %s\nDuration: %s\nViews: %s\nRating: %s" % (unicode(mfrom.getResource()),
+ entry.media.player.url,
+ entry.media.title.text,
+ entry.media.category[0].text,
+ entry.media.keywords.text,
+ duration,
+ entry.statistics.view_count,
+ entry.rating.average)
+ brathuhn.sendMsg(brathuhn.ROOM,txt,'groupchat')
+brathuhn.addGroupListener(youtube_logger)
+
+
+
+
From 0654e6d1e63c40febbdcd06a1b68ec974e430ece Mon Sep 17 00:00:00 2001
From: vu
Date: Tue, 15 Jan 2013 22:12:53 +0100
Subject: [PATCH 2/2] small change, big impact: removed a '-'
---
modules/active/boggle.py | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/modules/active/boggle.py b/modules/active/boggle.py
index bc43a78..7d737f7 100644
--- a/modules/active/boggle.py
+++ b/modules/active/boggle.py
@@ -30,8 +30,8 @@ DICE.append(['N','O','O','T','U','W'])
DICE.append(['O','O','O','T','T','U'])
bog_kern = [[-1,-1], [-1, 0], [-1, 1],
- [ 0,-1] , [ 0, 1],
- [ 1,-1], [ 1, 0], [ 1,-1]]
+ [ 0,-1] , [ 0, 1],
+ [ 1,-1], [ 1, 0], [ 1, 1]]
bog_current = "asdf";