Created
July 7, 2016 13:45
-
-
Save vlastachu/4c0869c666797d1423a4c15765f5bef0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import json | |
from subprocess import * | |
from collections import Counter | |
import matplotlib | |
import matplotlib.pyplot as plt | |
import matplotlib.dates as mdates | |
from scipy.interpolate import splev, splrep, InterpolatedUnivariateSpline | |
import datetime | |
import time | |
def getWordCounter(word): | |
proc = Popen(["/home/vlastachu/dev/tg/bin/telegram-cli","--json"], stdin=PIPE, stdout=PIPE) | |
s = proc.communicate("search " + word + "\n\0")[0] | |
data = json.loads(s.splitlines()[-3][3:]) # load | |
print s.splitlines()[-3] | |
data = filter(lambda x: x["from"]["id"] == "$0100000057e46e08ab13293848bb299b", data) # kostya | |
c = Counter() | |
for item in data: | |
date = datetime.datetime.fromtimestamp(int(item["date"])) | |
date = date.replace(hour=0, minute=0, second=0) | |
c[date] += 1 | |
proc.wait() | |
return c | |
def getXYForWords(words): | |
c = Counter() | |
for word in words: | |
c.update(getWordCounter(word)) | |
time.sleep(3) | |
keys = sorted(c) | |
values = [c[x] for x in keys] | |
x = matplotlib.dates.date2num(keys).tolist() | |
print x | |
# fill zeros in empty places | |
if len(x) != 0: | |
for i in range(int(min(x)),int(max(x))): | |
if not float(i) in x: | |
values.insert(i-int(min(x)), 0) | |
x.insert(i-int(min(x)), float(i)) | |
# fill middle values for nice curve | |
newvalues = [] | |
newx = [] | |
for i in range(int(max(x) - min(x))): | |
newvalues.append(values[i]) | |
newvalues.append((values[i] + values[i+1]) / 2.0) | |
newx.append(x[i]) | |
newx.append(x[i]+0.5) | |
newvalues.append(values[-1]) | |
newx.append(x[-1]) | |
values = newvalues | |
x = newx | |
# fill array for spline axis | |
step = (max(x) - min(x))/1000 | |
newx = [min(x)] | |
for i in range(0, 1000): | |
newx.append(newx[-1]+step) | |
spl = InterpolatedUnivariateSpline(x, values, k=3) | |
y = spl(newx) | |
return (newx, y) | |
else: | |
return([], []) | |
(x0, y0) = getXYForWords(["мде", "mde"]) | |
time.sleep(3) | |
(x1, y1) = getXYForWords(["мда"]) | |
fig, ax = plt.subplots(1) | |
ax.plot(x0, y0, 'r', x1, y1, 'b--') | |
# use a more precise date string for the x axis locations in the toolbar | |
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d.%m')) | |
#plt.gca().xaxis.set_major_locator(mdates.DayLocator()) | |
# rotate and align the tick labels so they look better | |
fig.autofmt_xdate() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment