rupe_bot.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import rupes
  2. import twitter
  3. import time
  4. import logging
  5. def main():
  6. # Start logging
  7. logging.basicConfig(filename="/homec/organis2/rupes_murdoch/rupes.log", level='DEBUG')
  8. # Connect to API
  9. consumer_key = 'F1aosrucfBbYnJwZLfUrQLxh9'
  10. consumer_secret = open("/homec/organis2/rupes_murdoch/private/consumer-secret.txt").read().strip()
  11. access_key = '3008197577-8GDVTaizZa1k3vP9KfXSybO7FZfyOmUR8TTn0Re'
  12. access_secret = open("/homec/organis2/rupes_murdoch/private/access-token-secret.txt").read().strip()
  13. api = twitter.Api(consumer_key=consumer_key, consumer_secret=consumer_secret,
  14. access_token_key=access_key, access_token_secret=access_secret)
  15. # Get since_id
  16. since_ref = open("/homec/organis2/rupes_murdoch/since_ref.txt", "r")
  17. since_id = since_ref.read().strip()
  18. since_id = long(since_id) if since_id != 'None' else None
  19. since_ref.close()
  20. logging.info("Starting script with since_id=%s" % str(since_id))
  21. returncount = 200
  22. statuses = api.GetUserTimeline(screen_name='rupertmurdoch', count=returncount,
  23. since_id=since_id, trim_user=True, exclude_replies=True)
  24. if len(statuses) == 0:
  25. # logging.debug("No new statuses")
  26. return
  27. # Create a Deruped tweet
  28. deruped = None
  29. tid = None
  30. tweet = None
  31. # Iterate backwards until a valid tweet appears
  32. for s in reversed(statuses):
  33. tweet = s.text
  34. tid = s.id
  35. deruped = rupes.derupe(tweet)
  36. if deruped:
  37. break
  38. # Post tweet
  39. if deruped:
  40. api.PostUpdate(status=deruped, in_reply_to_status_id = tid)
  41. logging.info("Posted \"%s\" from tweet \"%s\"" % (deruped, tweet))
  42. else:
  43. logging.info("Skipping Tweet \"%s\"" % (tweet))
  44. # Save ID of last tweet dealt with
  45. since_ref = open("/homec/organis2/rupes_murdoch/since_ref.txt", "w")
  46. since_ref.write(str(tid))
  47. since_ref.close()
  48. if __name__=='__main__':
  49. main()