Change to get topic to be more flexible

This commit is contained in:
Mickael 2019-09-28 21:37:09 +02:00
parent cc1272bf08
commit aa0b0b4353
5 changed files with 36 additions and 72 deletions

View file

@ -1,4 +1,6 @@
import random
import re
def getColor():
r = random.randrange(255)
@ -6,6 +8,7 @@ def getColor():
b = random.randrange(255)
return 'rgb({:0},{:0},{:0})'.format(r, g, b)
def createElement(label, color, data):
return {
"label": label,
@ -13,34 +16,19 @@ def createElement(label, color, data) :
"data": data
}
def setTopicInfo(tsunamy, messagesDataTopic, id, length):
if tsunamy & Topics.GLOBAL:
messagesDataTopic[Topics.GLOBAL][id] += length
if tsunamy & Topics.PROJECT:
messagesDataTopic[Topics.PROJECT][id] += length
if tsunamy & Topics.DEMOCRACY:
messagesDataTopic[Topics.DEMOCRACY][id] += length
if tsunamy & Topics.ECOLOGY:
messagesDataTopic[Topics.ECOLOGY][id] += length
if tsunamy & Topics.TECHNOLOGY:
messagesDataTopic[Topics.TECHNOLOGY][id] += length
def setTopicsInfo(topics, messagesDataTopic, id, length):
for topic in topics:
if topic not in messagesDataTopic:
messagesDataTopic[topic] = [0] * 30
messagesDataTopic[topic][id] += length
def getTopic(channel):
value = Topics.GLOBAL
def getTopics(channel):
if 'description' in channel:
if channel['description'].find("#projet") != -1:
value |= Topics.PROJECT
if channel['description'].find("#democratie") != -1:
value |= Topics.DEMOCRACY
if channel['description'].find("#ecologie") != -1:
value |= Topics.ECOLOGY
if channel['description'].find("#technologie") != -1:
value |= Topics.TECHNOLOGY
return value
topics = re.findall(r"#(\w+)\W?", channel['description'])
if len(topics) == 0:
topics.append('global')
return topics
return ['global']
class Topics:
GLOBAL = 1 << 0
PROJECT = 1 << 1
DEMOCRACY = 1 << 2
ECOLOGY = 1 << 3
TECHNOLOGY = 1 << 4
if __name__ == "__main__":
print("not executable")

View file

@ -11,7 +11,7 @@ import random
from datetime import datetime, timedelta
from common.rocketchathelper import getAllChannels, getAllMessages, Connection
from common.savehelper import save
from common.charthelper import createElement, getColor, setTopicInfo, getTopic, Topics
from common.charthelper import createElement, getColor, setTopicsInfo, getTopics
begin = datetime.now()
end = datetime.now()
@ -25,13 +25,7 @@ def main():
labels = [None] * nbElements
messagesByChannel = []
usersByChannel = []
messagesDataTopic = {
Topics.GLOBAL: [0] * nbElements,
Topics.PROJECT: [0] * nbElements,
Topics.DEMOCRACY: [0] * nbElements,
Topics.ECOLOGY: [0] * nbElements,
Topics.TECHNOLOGY: [0] * nbElements,
}
messagesByTopic = {}
usersGlobal = []
now = datetime.now()
@ -42,13 +36,7 @@ def main():
"labels": labels,
"messagesByChannel": messagesByChannel,
"usersByChannel": usersByChannel,
"messagesByTopic": [
createElement("global", getColor(), messagesDataTopic[Topics.GLOBAL]),
createElement("project", getColor(), messagesDataTopic[Topics.PROJECT]),
createElement("democratie", getColor(), messagesDataTopic[Topics.DEMOCRACY]),
createElement("ecologie", getColor(), messagesDataTopic[Topics.ECOLOGY]),
createElement("technologie", getColor(), messagesDataTopic[Topics.TECHNOLOGY])
],
"messagesByTopic": messagesByTopic,
"usersGlobal": usersGlobal
}
@ -61,8 +49,8 @@ def main():
begin = date - timedelta(nbElements)
end = begin + timedelta(nbElements)
tsunamy = getTopic(channel)
topics = getTopics(channel)
print(topics)
messages = getAllMessages(rocket, channel['_id'], begindate= begin, enddate= end, count= channel["msgs"])
end = begin + timedelta(1)
@ -77,7 +65,7 @@ def main():
dataMess.append(length)
if length > 0:
setTopicInfo(tsunamy, messagesDataTopic, id, length)
setTopicsInfo(topics, messagesByTopic, id, length)
users = []
for mess in resultMess:

View file

@ -12,7 +12,7 @@ from datetime import datetime
from monthdelta import monthdelta
from common.rocketchathelper import getAllChannels, getAllMessages, Connection
from common.savehelper import save
from common.charthelper import createElement, getColor, setTopicInfo, Topics, getTopic
from common.charthelper import createElement, getColor, setTopicsInfo, getTopics
begin = datetime.now()
end = datetime.now()
@ -26,13 +26,7 @@ def main():
labels = [None] * 12
messagesByChannel = []
usersByChannel = []
messagesDataTopic = {
Topics.GLOBAL: [0] * 12,
Topics.PROJECT: [0] * 12,
Topics.DEMOCRACY: [0] * 12,
Topics.ECOLOGY: [0] * 12,
Topics.TECHNOLOGY: [0] * 12,
}
messagesByTopic = {}
usersGlobal = []
now = datetime.now()
@ -43,13 +37,7 @@ def main():
"labels": labels,
"messagesByChannel": messagesByChannel,
"usersByChannel": usersByChannel,
"messagesByTopic": [
createElement("global", getColor(), messagesDataTopic[Topics.GLOBAL]),
createElement("project", getColor(), messagesDataTopic[Topics.PROJECT]),
createElement("democratie", getColor(), messagesDataTopic[Topics.DEMOCRACY]),
createElement("ecologie", getColor(), messagesDataTopic[Topics.ECOLOGY]),
createElement("technologie", getColor(), messagesDataTopic[Topics.TECHNOLOGY])
],
"messagesByTopic": messagesByTopic,
"usersGlobal": usersGlobal
}
@ -64,8 +52,8 @@ def main():
messages = getAllMessages(rocket, channel['_id'], begindate= begin, enddate=date, count= channel['msgs'])
tsunamy = getTopic(channel)
topics = getTopics(channel)
print(topics)
for id in range(0, 12):
if uniqueUserGlobal[id] == None:
uniqueUserGlobal[id] = []
@ -77,7 +65,7 @@ def main():
dataMess.append(length)
if length > 0:
setTopicInfo(tsunamy, messagesDataTopic, id, length)
setTopicsInfo(topics, messagesByTopic, id, length)
users = []
for mess in resultMess:

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long