global by db
[rainbowstream.git] / rainbowstream / rainbow.py
index c1fb60e1e3ad318ac5bb011efdf4e82cc8977ae8..526437fee0a731e56e254972ec5a0b5f8c29e29d 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)
@@ -151,12 +158,16 @@ def view():
     """
     t = Twitter(auth=authen())
     user = g['stuff'].split()[0]
-    try:
-        count = int(g['stuff'].split()[1])
-    except:
-        count = HOME_TWEET_NUM
-    for tweet in reversed(t.statuses.user_timeline(count=count, screen_name=user)):
-        draw(t=tweet)
+    if user[0] == '@':
+        try:
+            count = int(g['stuff'].split()[1])
+        except:
+            count = HOME_TWEET_NUM
+        for tweet in reversed(t.statuses.user_timeline(count=count, screen_name=user[1:])):
+            draw(t=tweet)
+    else:
+        print(red('A name should begin with a \'@\''))
+        sys.stdout.write(g['decorated_name'])
 
 
 def tweet():
@@ -174,12 +185,14 @@ 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'])
 
 
 def delete():
@@ -189,10 +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'])
 
 
 def search():
@@ -200,14 +215,17 @@ def search():
     Search
     """
     t = Twitter(auth=authen())
-    rel = t.search.tweets(q='#' + g['stuff'])['statuses']
     h, w = os.popen('stty size', 'r').read().split()
-
-    printNicely(grey('*' * int(w) + '\n'))
-    print('Newest', SEARCH_MAX_RECORD, 'tweet: \n')
-    for i in xrange(5):
-        draw(t=rel[i], keyword=g['stuff'].strip())
-    printNicely(grey('*' * int(w) + '\n'))
+    if g['stuff'][0] == '#':
+        rel = t.search.tweets(q=g['stuff'])['statuses']
+        printNicely(grey('*' * int(w) + '\n'))
+        print('Newest', SEARCH_MAX_RECORD, 'tweet: \n')
+        for i in xrange(5):
+            draw(t=rel[i], keyword=g['stuff'].strip())
+        printNicely(grey('*' * int(w) + '\n'))
+    else:
+        print(red('A keyword should be a hashtag (like \'#AKB48\')'))
+        sys.stdout.write(g['decorated_name'])
 
 
 def friend():
@@ -244,16 +262,16 @@ def help():
     Hi boss! I'm ready to serve you right now!
     ----------------------------------------------------
     "home" will show your timeline. "home 7" will print 7 tweet.
-    "view bob" will show your friend @bob's home.
+    "view @bob" will show your friend @bob's home.
     "t oops" will tweet "oops" immediately.
-    "rep 12345 oops" will reply "oops" to tweet with id "12345" .
+    "rep 12345 oops" will reply "oops" to tweet with id "12345".
     "del 12345" will delete tweet with id "12345".
-    "s AKB48" will search for "AKB48" and return 5 newest tweet
-    "fr" will list out your following people
-    "fl" will list out your followers
-    "h" or "help" will print this help once again
-    "c" will clear the terminal
-    "q" will exit
+    "s #AKB48" will search for "AKB48" and return 5 newest tweet.
+    "fr" will list out your following people.
+    "fl" will list out your followers.
+    "h" or "help" will print this help once again.
+    "c" will clear the terminal.
+    "q" will exit.
     ----------------------------------------------------
     Have fun and hang tight!
     '''
@@ -356,6 +374,7 @@ def fly():
     Main function
     """
     get_decorated_name()
+
     p = Process(target=stream)
     p.start()
     g['stream_pid'] = p.pid