--- /dev/null
+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
from .colors import *
from .config import *
+from .db import *
g = {}
-
+db = RainbowDB()
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)
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'])
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():
Main function
"""
get_decorated_name()
+
p = Process(target=stream)
p.start()
g['stream_pid'] = p.pid
--- /dev/null
+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