improve search query
[rainbowstream.git] / rainbowstream / rainbow.py
index 6bd1720..de908e6 100644 (file)
@@ -122,7 +122,7 @@ def init(args):
     name = credential['name']
     if not get_config('PREFIX'):
         set_config('PREFIX', screen_name)
     name = credential['name']
     if not get_config('PREFIX'):
         set_config('PREFIX', screen_name)
-    g['original_name'] = screen_name[1:]
+    c['original_name'] = g['original_name'] = screen_name[1:]
     g['full_name'] = name
     g['decorated_name'] = lambda x: color_func(
         c['DECORATED_NAME'])('[' + x + ']: ')
     g['full_name'] = name
     g['decorated_name'] = lambda x: color_func(
         c['DECORATED_NAME'])('[' + x + ']: ')
@@ -249,13 +249,24 @@ def search():
     Search
     """
     t = Twitter(auth=authen())
     Search
     """
     t = Twitter(auth=authen())
-    g['stuff'] = g['stuff'].strip()
-    rel = t.search.tweets(q=g['stuff'])['statuses']
+    # Setup query
+    query = g['stuff'].strip()
+    type = c['SEARCH_TYPE']
+    if type not in ['mixed', 'recent', 'popular']:
+        type = 'mixed'
+    max_record = c['SEARCH_MAX_RECORD']
+    count = min(max_record, 100)
+    # Perform search
+    rel = t.search.tweets(
+        q=query,
+        type=type,
+        count=count
+    )['statuses']
+    # Return results
     if rel:
         printNicely('Newest tweets:')
     if rel:
         printNicely('Newest tweets:')
-        for i in reversed(xrange(c['SEARCH_MAX_RECORD'])):
-            draw(t=rel[i],
-                 keyword=g['stuff'])
+        for i in reversed(xrange(count)):
+            draw(t=rel[i], keyword=query)
         printNicely('')
     else:
         printNicely(magenta('I\'m afraid there is no result'))
         printNicely('')
     else:
         printNicely(magenta('I\'m afraid there is no result'))
@@ -461,8 +472,9 @@ def urlopen():
             return
         tid = c['tweet_dict'][int(g['stuff'])]
         tweet = t.statuses.show(id=tid)
             return
         tid = c['tweet_dict'][int(g['stuff'])]
         tweet = t.statuses.show(id=tid)
-        link_ary = [
-            u for u in tweet['text'].split() if u.startswith('http://')]
+        link_prefix = ('http://', 'https://')
+        link_ary = [u for u in tweet['text'].split()
+                    if u.startswith(link_prefix)]
         if not link_ary:
             printNicely(light_magenta('No url here @.@!'))
             return
         if not link_ary:
             printNicely(light_magenta('No url here @.@!'))
             return
@@ -522,18 +534,20 @@ def inbox():
 
     d = {}
     uniq_inbox = list(set(
 
     d = {}
     uniq_inbox = list(set(
-        [(m['sender_screen_name'],m['sender']['name']) for m in inbox]
+        [(m['sender_screen_name'], m['sender']['name']) for m in inbox]
     ))
     ))
-    uniq_sent= list(set(
-        [(m['recipient_screen_name'],m['recipient']['name']) for m in sent]
+    uniq_sent = list(set(
+        [(m['recipient_screen_name'], m['recipient']['name']) for m in sent]
     ))
     for partner in uniq_inbox:
         inbox_ary = [m for m in inbox if m['sender_screen_name'] == partner[0]]
     ))
     for partner in uniq_inbox:
         inbox_ary = [m for m in inbox if m['sender_screen_name'] == partner[0]]
-        sent_ary = [m for m in sent if m['recipient_screen_name'] == partner[0]]
+        sent_ary = [
+            m for m in sent if m['recipient_screen_name'] == partner[0]]
         d[partner] = inbox_ary + sent_ary
     for partner in uniq_sent:
         if partner not in d:
         d[partner] = inbox_ary + sent_ary
     for partner in uniq_sent:
         if partner not in d:
-            d[partner] = [m for m in sent if m['recipient_screen_name'] == partner[0]]
+            d[partner] = [
+                m for m in sent if m['recipient_screen_name'] == partner[0]]
     g['message_threads'] = print_threads(d)
 
 
     g['message_threads'] = print_threads(d)
 
 
@@ -543,11 +557,11 @@ def thread():
     """
     try:
         thread_id = int(g['stuff'])
     """
     try:
         thread_id = int(g['stuff'])
-        print_thread(g['message_threads'][thread_id],g['original_name'],g['full_name'])
-    except Exception as e:
-        print(e)
-        import traceback
-        print(traceback.format_exc())
+        print_thread(
+            g['message_threads'][thread_id],
+            g['original_name'],
+            g['full_name'])
+    except Exception:
         printNicely(red('No such thread.'))
 
 
         printNicely(red('No such thread.'))
 
 
@@ -556,19 +570,19 @@ def message():
     Send a direct message
     """
     t = Twitter(auth=authen())
     Send a direct message
     """
     t = Twitter(auth=authen())
-    user = g['stuff'].split()[0]
-    if user[0].startswith('@'):
-        try:
-            content = g['stuff'].split()[1]
-        except:
-            printNicely(red('Sorry I can\'t understand.'))
-        t.direct_messages.new(
-            screen_name=user[1:],
-            text=content
-        )
-        printNicely(green('Message sent.'))
-    else:
-        printNicely(red('A name should begin with a \'@\''))
+    try:
+        user = g['stuff'].split()[0]
+        if user[0].startswith('@'):
+            content = ' '.join(g['stuff'].split()[1:])
+            t.direct_messages.new(
+                screen_name=user[1:],
+                text=content
+            )
+            printNicely(green('Message sent.'))
+        else:
+            printNicely(red('A name should begin with a \'@\''))
+    except:
+        printNicely(red('Sorry I can\'t understand.'))
 
 
 def trash():
 
 
 def trash():
@@ -1274,8 +1288,8 @@ def help_messages():
     usage += s + grey(u'\u266A' + ' Direct messages \n')
     usage += s * 2 + light_green('inbox') + ' will show inbox messages. ' + \
         light_green('inbox 7') + ' will show newest 7 messages.\n'
     usage += s + grey(u'\u266A' + ' Direct messages \n')
     usage += s * 2 + light_green('inbox') + ' will show inbox messages. ' + \
         light_green('inbox 7') + ' will show newest 7 messages.\n'
-    usage += s * 2 + light_green('sent') + ' will show sent messages. ' + \
-        light_green('sent 7') + ' will show newest 7 messages.\n'
+    usage += s * 2 + light_green('thread 2') + ' will show full thread with ' + \
+        light_yellow('[thread_id=2]') + '.\n'
     usage += s * 2 + light_green('mes @dtvd88 hi') + ' will send a "hi" messege to ' + \
         magenta('@dtvd88') + '.\n'
     usage += s * 2 + light_green('trash 5') + ' will remove message with ' + \
     usage += s * 2 + light_green('mes @dtvd88 hi') + ' will send a "hi" messege to ' + \
         magenta('@dtvd88') + '.\n'
     usage += s * 2 + light_green('trash 5') + ' will remove message with ' + \
@@ -1638,7 +1652,7 @@ def listen():
             [''],  # open url
             ['fl', 'fr'],  # list
             [],  # inbox
             [''],  # open url
             ['fl', 'fr'],  # list
             [],  # inbox
-            [i for i in g['message_threads']],  #sent
+            [i for i in g['message_threads']],  # sent
             [],  # trash
             ['@'],  # whois
             ['@'],  # follow
             [],  # trash
             ['@'],  # whois
             ['@'],  # follow