Skip to content

Instantly share code, notes, and snippets.

@an9er
Created December 10, 2015 03:06
Show Gist options
  • Save an9er/401effc289face112c67 to your computer and use it in GitHub Desktop.
Save an9er/401effc289face112c67 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Breif Module Description: show the number of published news every day
# Created on Wednesday, 11 November 2015.
import datetime
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
from db_instance import db
def get_from_date(tag):
sql = 'select min(created_at) as fro from pre_weibo_status_150_events where tag = "%s"' % tag
c = db.query(sql)
return c[0]['fro']
def get_to_date(tag):
sql = 'select max(created_at) as sto from pre_weibo_status_150_events where tag = "%s"' % tag
c = db.query(sql)
return c[0]['sto']
def drawplot(tag):
global db
# --add start--
start_date = get_from_date(tag)
stop_date = get_to_date(tag)
days = (stop_date - start_date).days
print days
# --add end--
# start_date = datetime.datetime(2015, 10, 10)
sql = ('select count(*) as cnt from pre_weibo_status_150_events '
'where created_at>=%s and created_at<%s and ' + 'tag="%s"' % tag)
print sql
periods = [(start_date + datetime.timedelta(i)) for i in xrange(32)]
counts = []
dates = []
for i in periods:
ds = "%s" % i
dates.append(ds[5:10])
c = db.query(sql, i, i + datetime.timedelta(1))
counts.append(c[0]['cnt'])
plt.plot(periods, counts, 'ro', periods, counts, 'k')
plt.ylabel('count')
plt.xlabel('date')
plt.savefig(tag+'.png')
plt.close()
if __name__ == '__main__':
tag = '1_5'
drawplot(tag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment