Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added user search

  • Loading branch information...
commit baf2ac6c5d44ef29db2bab74a73891d67c2b726a 1 parent 6f09124
Hilary Mason authored
Showing with 53 additions and 6 deletions.
  1. +11 −0 lib/display.py
  2. BIN  lib/display.pyc
  3. +42 −6 s.py
View
11 lib/display.py
@@ -31,4 +31,15 @@ def display_tweets(self, tweets):
print tweet_text + ' ' + self.OKBLUE + t['_datetime'] + self.ENDC
else: # print without topics
print tweet_text
+
+ def display_users(self, users):
+
+ for t in users:
+ if t['_display']:
+ spacer = ' '.join(['' for i in range((self.MAX_TWITTER_USERNAME_LENGTH + 2) - len(t['_id']))])
+ tweet_text = self.OKGREEN + t['_id'] + self.ENDC + spacer + t['name'] + ' ' + t['location'] + ' ' + t['description']
+ if t.get('url', None): # print with url
+ print tweet_text + ' ' + self.OKBLUE + t['url'] + self.ENDC
+ else: # print without topics
+ print tweet_text
View
BIN  lib/display.pyc
Binary file not shown
View
48 s.py
@@ -22,10 +22,16 @@
class Search(object):
def __init__(self, options, args):
+ self.debug = options.debug
self.db = mongodb.connect('tweets')
- tweets = self.tweet_search(args, options.num)
- d = display.Display()
- d.display_tweets(tweets)
+ d = display.Display()
+
+ if options.user_search:
+ twitterers = self.user_search(options.user_search, int(options.num))
+ d.display_users(twitterers)
+ else:
+ tweets = self.tweet_search(args, int(options.num))
+ d.display_tweets(tweets)
def tweet_search(self, query_terms, num=10):
@@ -35,16 +41,46 @@ def tweet_search(self, query_terms, num=10):
t['_display'] = True
t['_datetime'] = datetime.datetime.strftime(t['created_at'], "%I:%M%p, %b %d, %Y %Z")
tweets.append(t)
+
+ if self.debug:
+ print "%s total results" % (len(tweets))
+
+ return tweets[:num]
+
+ def user_search(self, user_terms, num=10):
+ r = re.compile(user_terms, re.I)
+ tweets = []
+
+ # search username
+ for t in self.db['users'].find(spec={'_id': r}).sort('_updated', direction=pymongo.DESCENDING):
+ t['_display'] = True
+ t['_datetime'] = datetime.datetime.strftime(t['_updated'], "%I:%M%p, %b %d, %Y %Z")
+ tweets.append(t)
+
+ usernames = [t['_id'] for t in tweets]
+
+ # search name
+ for t in self.db['users'].find(spec={'name': r}).sort('_updated', direction=pymongo.DESCENDING):
+ if t['_id'] not in usernames:
+ t['_display'] = True
+ t['_datetime'] = datetime.datetime.strftime(t['_updated'], "%I:%M%p, %b %d, %Y %Z")
+ tweets.append(t)
+
+
+
+
+
+ if self.debug:
+ print "%s total results" % (len(tweets))
return tweets[:num]
-
if __name__ == "__main__":
- parser = OptionParser("usage: %prog [options]")
+ parser = OptionParser("usage: %prog [options] query terms")
parser.add_option("-d", "--debug", dest="debug", action="store_true", default=False, help="set debug mode = True")
parser.add_option("-n", "--num", dest="num", action="store", default=10, help="number of tweets to retrieve")
- # parser.add_option("-t", "--topic", dest="topic", action="store", default=None, help="show one topic only")
+ parser.add_option("-u", "--user", dest="user_search", action="store", default=None, help="search users")
(options, args) = parser.parse_args()
t = Search(options, args)
Please sign in to comment.
Something went wrong with that request. Please try again.