Guest User

Untitled

a guest
May 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. #Import the necessary methods from tweepy library
  2. import tweepy, codecs
  3. import pymysql
  4. import time
  5.  
  6. #Variables that contains the user credentials to access Twitter API
  7. access_token = "1224754879-W3GemB5jCRm4VTrPPS13rgLhPGLpzoiEt1iARa8"
  8. access_token_secret = "aJEUW4k9fBIQ5rcYYRGpH0UzzXByV6rMoYUSIJHYqxYwe"
  9. consumer_key = "yxAbP3ypx6AbO8YfcKzsIVkdW"
  10. consumer_secret = "hFOb7Ss4qY9mlQLwdm1A32jcO0y4ivuzG6brY2VxFORosVEw0s"
  11.  
  12. #Authentication
  13. auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  14. auth.set_access_token(access_token, access_token_secret)
  15. api = tweepy.API(auth)
  16.  
  17. #Declare Connection
  18. conn = pymysql.connect(host='localhost', port='', user='root', passwd='', db='test', use_unicode=True, charset="utf8mb4")
  19. cur = conn.cursor()
  20.  
  21. #Get Current Date Time
  22. curdatetime = time.strftime("%Y-%m-%d %H:%M:%S")
  23.  
  24. cur.execute("DELETE FROM tweet order by id desc LIMIT 500")
  25.  
  26. #Get last id from table tweet
  27. last_id = 0
  28. cur.execute("SELECT MAX(id) FROM tweet")
  29. result = cur.fetchall()
  30. for row in result:
  31. last_id = row[0]
  32. print ("Last ID : " + str(last_id))
  33.  
  34. #Get Number of Tweet
  35. user = api.get_user(108543358)
  36. print ("Name:", user.name)
  37. print ("Name:", user.screen_name)
  38. print ("Number of tweets: " + str(user.statuses_count))
  39. print ("followers_count: " + str(user.followers_count))
  40. print ("Account location: ", user.location)
  41. print ("Account created at: ", user.created_at)
  42.  
  43.  
  44. n = 0
  45. for Tweet in tweepy.Cursor(api.user_timeline, id=108543358, q = "#Gempa", lang = id, result_type = "recent", since_id = last_id).items(3):
  46. print ("*****" + str(i) +"*****")
  47. print ("ID: " + Tweet.id_str)
  48. print ("Text: " + str(Tweet.text.encode("utf-8")))
  49. print ("Retweet Count: " + str(Tweet.retweet_count))
  50. print ("Favorite Count: " + str(Tweet.favorite_count))
  51. print ("Date Time: " + str(Tweet.created_at))
  52. #print (str(Tweet.location)) #how to get geolocation data for mapping ?
  53. print ("************")
  54.  
  55. n = n + 1
  56. cur.execute("INSERT INTO tweet (no, id, text, retweet_count, favourite_count, date_time) VALUES (%s, %s,%s,%s,%s,%s)",
  57. (str(n), Tweet.id_str, Tweet.text.encode("utf-8"), str(Tweet.retweet_count), str(Tweet.favorite_count), str(Tweet.created_at)))
  58.  
  59.  
  60. conn.commit()
  61. cur.close()
  62. conn.close()
Add Comment
Please, Sign In to add comment