<commit_before># coding=utf8
"""
asl.py - Willie Freies Labor Activity Streams Lite Module
Licensed under a Mozilla Public License 2.0.
"""
from willie.module import commands, interval
import urllib2, json, datetime, os, pickle

ASL_QUERY = '-wiki.*&-sensor.traffic-light&-sensor.mate-o-meter&-twitter.retweet'


def setup(self):
	"""Performs startup tasks."""
	fn = self.nick + '-' + self.config.host + '.asl.p'
	self.asl_filename = os.path.join(self.config.dotdir, fn)
	if not os.path.exists(self.asl_filename):
		try:
			f = open(self.asl_filename, 'wb')
		except OSError:
			pass
		else:
			# write current id as last id
			lastId = asl(self.config.asl.asl_query)[0]['id']
			pickle.dump(lastId, f)
			f.close()


def configure(config):
	"""
	|  [asl]    | example             | purpose         |
	| ----------| ------------------- | --------------- |
	| asl_query | last_id=%d%sensor.* | Sets ASL filter |
	"""
	if config.option('Configure ASL module', False):
		config.add_section('asl')
		config.interactive_add('asl', 'asl_query', 'ASL query')


def asl(argStr=''):
	"""Returns ASL results matching the filter arg string."""
	try:
		request = urllib2.Request('http://asl.hickerspace.org/asl.json?%s' \
			% argStr)
		response = json.load(urllib2.urlopen(request))
		return response['results']
	except urllib2.URLError as e:
		return []
	return messages


def getAslUpdates(bot):
	"""Checks for new ASL messages and announces them."""
	with open(bot.asl_filename) as f:
		lastId = pickle.load(f)
		for item in asl('%s&%s' % ("last_id=%d" % lastId, bot.config.asl.asl_query)):
			lastId = item['id'] if item['id'] > lastId else lastId

			# ignore updates older than 90 minutes
			date = datetime.datetime.strptime(item['datetime'], '%Y-%m-%d %H:%M:%S')
			if date < datetime.datetime.now() - datetime.timedelta(minutes=90):
				continue

			author = 'by %s ' % item['person'] if item['person'] else ''
			message = '[%s] %s %s(%s)' % (item['service'].title(), item['content'], author, item['url'])

			# announce in all channels
			for chan in bot.channels:
				bot.msg(chan, message)

			# change topic if room status changed
			if item['service'] == 'sensor' and item['type'] == 'room':
				bot.msg(chan, r'\topic Freies Labor Hildesheim - %s - https://freieslabor.org' % item['content'])

		pickle.dump(lastId, open(bot.asl_filename, 'wb'))


@interval(30)
def intervalAsl(bot):
	"""Queries ASL updates automatically."""
	getAslUpdates(bot)


@commands('events')
def queryEvents(bot, trigger):<commit_msg>setting topic according to room state<commit_after># coding=utf8
"""
asl.py - Willie Freies Labor Activity Streams Lite Module
Licensed under a Mozilla Public License 2.0.
"""
from willie.module import commands, interval
import urllib2, json, os, pickle
from datetime import datetime, timedelta

ASL_QUERY = '-wiki.*&-sensor.traffic-light&-sensor.mate-o-meter&-twitter.retweet'


def setup(self):
	"""Performs startup tasks."""
	fn = self.nick + '-' + self.config.host + '.asl.p'
	self.asl_filename = os.path.join(self.config.dotdir, fn)
	if not os.path.exists(self.asl_filename):
		try:
			f = open(self.asl_filename, 'wb')
		except OSError:
			pass
		else:
			# write current id as last id
			lastId = asl(self.config.asl.asl_query)[0]['id']
			pickle.dump(lastId, f)
			f.close()


def configure(config):
	"""
	|  [asl]    | example             | purpose         |
	| ----------| ------------------- | --------------- |
	| asl_query | last_id=%d%sensor.* | Sets ASL filter |
	"""
	if config.option('Configure ASL module', False):
		config.add_section('asl')
		config.interactive_add('asl', 'asl_query', 'ASL query')


def asl(argStr=''):
	"""Returns ASL results matching the filter arg string."""
	try:
		request = urllib2.Request('http://asl.hickerspace.org/asl.json?%s' \
			% argStr)
		response = json.load(urllib2.urlopen(request))
		return response['results']
	except urllib2.URLError as e:
		return []
	return messages


def getAslUpdates(bot):
	"""Checks for new ASL messages and announces them."""
	with open(bot.asl_filename) as f:
		lastId = pickle.load(f)
		query = '%s&%s' % ("last_id=%d" % lastId, bot.config.asl.asl_query)
		for item in asl(query):
			lastId = item['id'] if item['id'] > lastId else lastId

			# ignore updates older than 90 minutes
			date = datetime.strptime(item['datetime'], '%Y-%m-%d %H:%M:%S')
			if date < datetime.now() - timedelta(minutes=90):
				continue

			author = 'by %s ' % item['person'] if item['person'] else ''
			message = '[%s] %s %s(%s)' % \
				(item['service'].title(), item['content'], author, item['url'])

			# announce in all channels
			for chan in bot.channels:
				bot.msg(chan, message)

				# change topic if room status changed
				if item['service'] == 'sensor' and item['type'] == 'room':
					since = date.strftime('%a, %H:%M')
					status = "%s since %s" % (item['content'][:-1], since)
					bot.msg('ChanServ', 'TOPIC %s Freies Labor ' % chan \
						+ 'Hildesheim - %s - https://freieslabor.org' % \
						status)

		pickle.dump(lastId, open(bot.asl_filename, 'wb'))


@interval(30)
def intervalAsl(bot):
	"""Queries ASL updates automatically."""
	getAslUpdates(bot)


@commands('events')
def queryEvents(bot, trigger):
{'fraction_non_alphanumeric': 0.18103282779804186, 'fraction_numerical': 0.0055672873872144365, 'mean_word_length': 3.550218340611354, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6366527', 'n_tokens_mistral': 2017, 'n_tokens_neox': 1818, 'n_words': 449}