Thanks to help from Jasmin.
In [299]:
from sqlalchemy import *
import datetime
import mediacloud
mc = mediacloud.api.MediaCloud('MEDIA_CLOUD_KEY')
In [300]:
# add `echo=True` to see log statements of all the SQL that is generated
engine = create_engine('sqlite:///:memory:') # just save the db in memory for now (ie. not on disk)
metadata = MetaData()
def callMediaCloud(SearchPhrase):
return mc.sentenceCount('('+SearchPhrase+')', solr_filter=[mc.publish_date_query(
datetime.date( 2010, 1, 1), datetime.date( 2015, 1, 1) ), 'media_sets_id:1' ])['count']
# define a table to use
queries = Table('queries', metadata,
Column('id', Integer, primary_key=True),
Column('keywords', String(400), nullable=False),
Column('timestamp', DateTime, default=datetime.datetime.now),
Column('count', Integer),
)
metadata.create_all(engine) # and create the tables in the database
In [301]:
insert_stmt = queries.insert()
str(insert_stmt) # see an example of what this will do
Out[301]:
In [302]:
insert_stmt = queries.insert().values(keywords="forest AND fire", count=callMediaCloud("forest AND fire"))
str(insert_stmt)
Out[302]:
In [303]:
db_conn = engine.connect()
result = db_conn.execute(insert_stmt)
result.inserted_primary_key # print out the primary key it was assigned
Out[303]:
In [304]:
insert_stmt = queries.insert().values(keywords="deforestation", count=callMediaCloud("deforestation"))
result = db_conn.execute(insert_stmt)
result.inserted_primary_key # print out the primary key it was assigned
Out[304]:
In [305]:
from sqlalchemy.sql import select
select_stmt = select([queries])
results = db_conn.execute(select_stmt)
for row in results:
print row
In [306]:
select_stmt = select([queries]).where(queries.c.id==1)
for row in db_conn.execute(select_stmt):
print row
In [307]:
select_stmt = select([queries]).where(queries.c.keywords.like('p%'))
for row in db_conn.execute(select_stmt):
print row
In [308]:
counter = 0
select_stmt = select([queries])
results = db_conn.execute(select_stmt)
for row in results:
counter+=row['count']
print counter
In [309]:
import datetime
from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.hybrid import hybrid_property
Base = declarative_base()
In [310]:
class Query(Base):
__tablename__ = 'queries'
id = Column(Integer, primary_key=True)
keywords = Column(String(400))
timestamp = Column(DateTime,default=datetime.datetime.now)
count = Column(Integer)
@hybrid_property
def count(self):
return callMediaCloud(self.keywords)
def __repr__(self):
return "<Query(keywords='%s' count='%d')>" % (self.keywords, self.count)
Query.__table__
Out[310]:
In [311]:
engine = create_engine('sqlite:///:memory:') # just save the db in memory for now (ie. not on disk)
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
my_session = Session()
In [312]:
query0 = Query(keywords="wherefore")
query1 = Query(keywords="insulate")
query2 = Query(keywords="electromagnetism")
query3 = Query(keywords="jejune")
query4 = Query(keywords="provocateur")
my_session.add_all([query0,query1,query2,query3,query4])
my_session.commit()
totalCount=0
for q in my_session.query(Query).order_by(Query.timestamp):
print q
totalCount+=q.count
print "Total Count is: " + str(totalCount)
In [ ]:
In [ ]: