Krenair

twittergrep for crow

Mar 30th, 2012
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 1.96 KB | None | 0 0
  1. diff --git a/plugins/twitter.py b/plugins/twitter.py
  2. index 68238e3..a4041f3 100644
  3. --- a/plugins/twitter.py
  4. +++ b/plugins/twitter.py
  5. @@ -132,3 +132,48 @@ def twitter(inp):
  6.      screen_name = tweet.find(screen_name).text
  7.  
  8.      return "%s %s: %s" % (time, screen_name, text)
  9. +
  10. +def twittergrep(inp):
  11. +    ".twittergrep <user> <search> -- gets last tweet by this user which includes the search string"
  12. +
  13. +    if inp.strip().find(' ') == -1:
  14. +        return 'error: invalid request'
  15. +
  16. +    name = inp[:inp.find(' ')]
  17. +    search = inp[inp.find(' ') + 1:].lower()
  18. +
  19. +    try:
  20. +        tweets = http.get_xml('http://twitter.com/statuses/user_timeline/%s.xml' % name)
  21. +    except http.HTTPError, e:
  22. +        errors = {400: 'bad request (ratelimited?)',
  23. +                401: 'tweet is private',
  24. +                403: 'tweet is private',
  25. +                404: 'invalid user/id',
  26. +                500: 'twitter is broken',
  27. +                502: 'twitter is down ("getting upgraded")',
  28. +                503: 'twitter is overloaded (lol, RoR)'}
  29. +        if e.code == 404:
  30. +            return 'error: invalid username'
  31. +        if e.code in errors:
  32. +            return 'error: ' + errors[e.code]
  33. +        return 'error: unknown %s' % e.code
  34. +    except http.URLerror, e:
  35. +        return 'error: timeout'
  36. +
  37. +    if not tweets.findall('status'):
  38. +        return 'error: user has no tweets'
  39. +
  40. +    found = False
  41. +    alltweets = tweets.findall('status')
  42. +    for tweet in alltweets:
  43. +        if search in tweet.find('text').text.lower():
  44. +            found = True
  45. +            break
  46. +
  47. +    if not found:
  48. +        return 'error: none of the user\'s last %s tweets have this string' % len(alltweets)
  49. +
  50. +    time = strftime('%Y-%m-%d %H:%M:%S', strptime(tweet.find('created_at').text, '%a %b %d %H:%M:%S +0000 %Y'))
  51. +    text = unescape_xml(tweet.find('text').text.replace('\n', ''))
  52. +    return "%s %s: %s" % (time, tweet.find('user/screen_name').text, text)
Advertisement
Add Comment
Please, Sign In to add comment