daily pastebin goal
66%
SHARE
TWEET

Untitled

a guest Dec 12th, 2016 9 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # thirstea
  2. # Richard Lewei Huang, Zhang Zhan
  3. # Digital Farm, NYU Shanghai Interactive Media Arts
  4.  
  5.  
  6. from twython import Twython
  7. twitter = Twython("2Vnnuk7ApcWXDYVc1Fw0Bncpp", "0YA8vvaJ9stjfmWYrz39df1vPqOWSqkTsPVdBiIwL1c1Uk02XV",
  8.                   "804236407212417024-6b0rWjuUnKQ4wdzDPRUhv3QVs3ltDPW", "wbzNgGbFFskTHj9uksgSn8AfYNiFYTBX9MNPDPuAwWcUn")
  9. import schedule
  10. import time
  11. import serial
  12. import urllib
  13. from bs4 import BeautifulSoup
  14. import requests
  15. import random
  16. import twittertemplates
  17.  
  18. camera_address = "http://10.209.7.224:8080/"
  19. ser = serial.Serial('/dev/ttyACM0', 9600)
  20. newfollowers = 0
  21. deltafollowers = 0
  22.  
  23. def get_follower_count():
  24.     print ("getting number of followers...")
  25.     followers = twitter.get_followers_ids(screen_name = "ThirsteaPlant")
  26.     followers_count = len(followers['ids'])
  27.     return followers_count
  28.  
  29. def get_follower_count_soup():
  30.     print ("getting number of followers via BeautifulSoup...")
  31.     username='thirsteaplant'
  32.     url = 'https://www.twitter.com/'+username
  33.     r = requests.get(url)
  34.     soup = BeautifulSoup(r.content)
  35.     f = soup.find('li', class_="ProfileNav-item--followers")
  36.     title = f.find('a')['title']
  37.     num_followers = int(title.split(' ')[0].replace(',',''))
  38.     return num_followers
  39.  
  40. def get_photo():
  41.     print ("getting photo from phone...")
  42.     photo_file = urllib.URLopener()
  43.     photo_file.retrieve(camera_address + "shot.jpg", "shot.jpg")
  44.     print ("photo download successful. ")
  45.     #photo download automatically overwrites the last photo so no need to handle overwrite issues.
  46.  
  47. def post_twitter_with_photo(text, photo_name):
  48.     photo = open(photo_name, 'rb')
  49.     try:
  50.         response = twitter.upload_media(media = photo)
  51.         twitter.update_status(status=text, media_ids = [response['media_id']])
  52.     except TwythonError as e:
  53.         print e
  54.    
  55. def water_plant(seconds):
  56.     # water the plant by turning on the motor via serial, then pause the program for
  57.     # [seconds] before turning the motor off via serial. With every follower the plant
  58.     # gets 30 seconds of water - which is not a lot.
  59.     print("watering plant")
  60.     ser.write("a")
  61.     time.sleep(seconds * 30 / 2) #*30/2
  62.     get_photo() # try to grab a photo while the water is running
  63.     time.sleep(seconds * 30 / 2) #*30/2
  64.     ser.write("b")
  65.  
  66. def update_task():
  67.     global newfollowers
  68.     global deltafollowers
  69.     global existingfollowers
  70.     print ("updating twitter follower information")
  71.     get_photo()
  72.     print ("getting photos")
  73.     newfollowers = get_follower_count()
  74.     print ("latest followers count: ")
  75.     print (newfollowers)
  76.     deltafollowers = newfollowers - existingfollowers
  77.     print ("new followers: ")
  78.     print (deltafollowers)
  79.    
  80.     if (deltafollowers > 0):
  81.         water_plant( deltafollowers )
  82.         post_twitter_with_photo("just got " + str(deltafollowers) + " new followers. " + random.choice(twittertemplates.remarks) , "shot.jpg")
  83.         existingfollowers = newfollowers
  84.     else:
  85.         print ("no new followers, complaining...")
  86.         post_twitter_with_photo( random.choice(twittertemplates.remarks), "shot.jpg" )
  87.  
  88. #initialising data and welcome output
  89. print ("Thirstea Plant Twitter Bot initialising...")
  90. newfollowers = get_follower_count()
  91. print ("currently I have this number of followers")
  92. print newfollowers
  93. existingfollowers = newfollowers
  94.  
  95. deltafollowers = newfollowers - existingfollowers
  96. print ("deltafollowers")
  97. print deltafollowers
  98.  
  99. schedule.every(2).minutes.do(update_task)
  100.  
  101. while True:
  102.     schedule.run_pending()
  103.     time.sleep(1)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top