Commits

Anonymous committed baf8369

new

Comments (0)

Files changed (2)

+#-------------------------------------------------------------------------------
+# Name:        saveEMA.py
+# Purpose:
+#
+#
+# Created:     19/01/2011
+# Copyright:   (c) Ravi Kotecha 2011
+# Licence:     WTFPL
+
+#-------------------------------------------------------------------------------
+#!/usr/bin/env python
+from __future__ import with_statement
+import tweetstream
+import getpass
+import sqlalchemy
+from sqlalchemy.ext.declarative import declarative_base, synonym_for
+from sqlalchemy import func
+from sqlalchemy.orm import sessionmaker
+from sqlalchemy.schema import *
+from sqlalchemy.types import *
+from GChartWrapper import Pie
+__author__ = 'kotecha.ravi@gmail.com'
+
+engine = sqlalchemy.create_engine("sqlite:///saveEMA.s3db")
+Base = declarative_base()
+class Tweets(Base):
+    __tablename__ = 'tweets'
+    id = Column(Integer, primary_key=True)
+    body = Column(String)
+    user = Column(String)
+    source = Column(String)
+
+    @synonym_for('source')
+    @property
+    def client(self):
+        if '>' in self.source:
+            l = self.source
+            return l[l.find('>')+1:l.rfind('<')]
+        else:
+            return self.source
+
+
+    def __init__(self, id, user, body, source):
+        self.id = id
+        self.user = user
+        self.body = body
+        self.source = source
+def get_chart(session):
+    stats = session.query(func.count(Tweets.Tweets), Tweets.source).group_by(Tweets.Tweets).all()
+    print stats
+    pass
+
+def main():
+    Base.metadata.create_all(engine)
+    Session = sessionmaker(bind=engine)
+    Session.configure(bind=engine)
+    session = Session()
+    tweet_count = session.query(Tweets).count()
+    print "Number of tweets in db: %s" % tweet_count
+
+    pw = getpass.getpass()
+    words = ['#saveEMA']
+    with tweetstream.TrackStream("r4vi", pw, words) as stream:
+        for tweet in stream:
+            try:
+                t = Tweets(tweet['id'], tweet['user']['screen_name'],tweet['text'],tweet['source'])
+                session.add(t)
+                try:
+                    session.commit()
+                except (sqlalchemy.exc.IntegrityError):
+                    pass
+
+                try:
+                    print "@"+tweet['user']['screen_name']+"=>\t", tweet['text'],tweet['created_at']
+                except:
+                    pass
+            except (KeyboardInterrupt, SystemExit):
+                print get_chart(session)
+                raise
+
+if __name__ == '__main__':
+    main()
Binary file added.