global by db
authorOrakaro <nhatminh_179@hotmail.com>
Tue, 27 May 2014 15:11:05 +0000 (00:11 +0900)
committerOrakaro <nhatminh_179@hotmail.com>
Tue, 27 May 2014 15:11:05 +0000 (00:11 +0900)
rainbowstream/db.py [new file with mode: 0644]
rainbowstream/rainbow.db [new file with mode: 0644]
rainbowstream/rainbow.py
rainbowstream/table_def.py [new file with mode: 0644]
requirements.txt

diff --git a/rainbowstream/db.py b/rainbowstream/db.py
new file mode 100644 (file)
index 0000000..d81f5ae
--- /dev/null
@@ -0,0 +1,30 @@
+from sqlalchemy import create_engine
+from sqlalchemy.orm import sessionmaker
+from table_def import Map
+
+
+class RainbowDB():
+
+    engine=None
+
+    def __init__(self):
+        self.engine = create_engine('sqlite:///rainbow.db', echo=False)
+
+    def store(self, tweet_id):
+        Session = sessionmaker(bind=self.engine)
+        session = Session()
+        m = Map(tweet_id)
+        session.add(m)
+        session.commit()
+
+    def rainbow_query(self, rid):
+        Session = sessionmaker(bind=self.engine)
+        session = Session()
+        res = session.query(Map).filter("rainbow_id =:rid").params(rid=rid).all()
+        return res
+
+    def tweet_query(self, tid):
+        Session = sessionmaker(bind=self.engine)
+        session = Session()
+        res = session.query(Map).filter("tweet_id =:tid").params(tid=tid).all()
+        return res
diff --git a/rainbowstream/rainbow.db b/rainbowstream/rainbow.db
new file mode 100644 (file)
index 0000000..fcd1099
Binary files /dev/null and b/rainbowstream/rainbow.db differ
index 3a6c7e3..526437f 100644 (file)
@@ -21,9 +21,10 @@ from dateutil import parser
 
 from .colors import *
 from .config import *
+from .db import *
 
 g = {}
-
+db = RainbowDB()
 
 def draw(t, keyword=None):
     """
@@ -38,9 +39,15 @@ def draw(t, keyword=None):
     date = parser.parse(created_at)
     time = date.strftime('%Y/%m/%d %H:%M:%S')
 
+    res = db.tweet_query(tid)
+    if not res:
+        db.store(tid)
+        res = db.tweet_query(tid)
+    rid = res[0].rainbow_id
+
     # Format info
     user = cycle_color(name) + grey(' ' + '@' + screen_name + ' ')
-    meta = grey('[' + time + '] [id=' + str(tid) + ']')
+    meta = grey('[' + time + '] [id=' + str(rid) + ']')
     tweet = text.split()
     # Highlight RT
     tweet = map(lambda x: grey(x) if x == 'RT' else x, tweet)
@@ -178,10 +185,11 @@ def reply():
     t = Twitter(auth=authen())
     try:
         id = int(g['stuff'].split()[0])
-        user = t.statuses.show(id=id)['user']['screen_name']
+        tid = db.rainbow_query(id)[0].tweet_id
+        user = t.statuses.show(id=tid)['user']['screen_name']
         status = ' '.join(g['stuff'].split()[1:])
         status = '@' + user + ' ' + status.decode('utf-8')
-        t.statuses.update(status=status, in_reply_to_status_id=id)
+        t.statuses.update(status=status, in_reply_to_status_id=tid)
     except:
         print(red('Sorry I can\'t understand.'))
         sys.stdout.write(g['decorated_name'])
@@ -194,11 +202,12 @@ def delete():
     t = Twitter(auth=authen())
     try:
         id = int(g['stuff'].split()[0])
-        t.statuses.destroy(id=id)
+        tid = db.rainbow_query(id)[0].tweet_id
+        t.statuses.destroy(id=tid)
         print(green('Okay it\'s gone.'))
     except:
         print(red('Sorry I can\'t delete this tweet for you.'))
-        sys.stdout.write(g['decorated_name'])
+    sys.stdout.write(g['decorated_name'])
 
 
 def search():
@@ -365,6 +374,7 @@ def fly():
     Main function
     """
     get_decorated_name()
+
     p = Process(target=stream)
     p.start()
     g['stream_pid'] = p.pid
diff --git a/rainbowstream/table_def.py b/rainbowstream/table_def.py
new file mode 100644 (file)
index 0000000..b6d14c5
--- /dev/null
@@ -0,0 +1,17 @@
+from sqlalchemy import *
+from sqlalchemy.ext.declarative import declarative_base
+
+engine = create_engine('sqlite:///rainbow.db', echo=False)
+Base = declarative_base()
+
+class Map(Base):
+
+    __tablename__ = "map"
+
+    rainbow_id = Column(Integer, primary_key=True)
+    tweet_id = Column(Integer)
+
+    def __init__(self, tweet_id):
+        self.tweet_id = tweet_id
+
+Base.metadata.create_all(engine)
\ No newline at end of file
index 4ec9a93..c9292f3 100644 (file)
@@ -4,3 +4,4 @@ python-dateutil==2.2
 six==1.6.1
 termcolor==1.1.0
 twitter==1.14.3
+SQLAlchemy=0.9.4
\ No newline at end of file