Guest User

Untitled

a guest
Oct 28th, 2017
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.44 KB | None | 0 0
  1. # encoding: utf-8
  2.  
  3. import datetime
  4. import re
  5.  
  6. from django.core.management import setup_environ
  7. import settings
  8. setup_environ(settings)
  9. from tweetmarks.tweetmark.models import *
  10. from django.db.models import Q
  11.  
  12. from getpass import getpass
  13. from textwrap import TextWrapper
  14. import tweepy
  15. import time
  16. import urllib
  17. import BeautifulSoup
  18. from time import sleep
  19.  
  20. CONSUMER_KEY = '6xbOX97j93m6HuNUqaw'
  21. CONSUMER_SECRET = 'eE6PzQ59O358U8ToTb7PlZSGS5qcQhSLjzSC3fYTy5Y'
  22. key = '302523037-FgkGWPIT9cnxsREroEnlOGrcjfas7pm3qw93P65q'
  23. secret = 'lYbRzbWS2kATGxmTHFJPK47X6vc131W2Mt27F5Ko9M'
  24. auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
  25. auth.set_access_token(key, secret)
  26. api = tweepy.API(auth)
  27. #public_tweets = tweepy.api.public_timeline()
  28. #for tweet in public_tweets:
  29. # print tweet.text
  30. followers = api.followers_ids()
  31. print(followers)
  32.  
  33. class StreamWatcherListener(tweepy.StreamListener):
  34. def on_status(self, status):
  35. tweet_urls = re.findall(r"https?://\S+", status.text, re.IGNORECASE)
  36. if tweet_urls:
  37. try:
  38. db_user, created_user = User.objects.get_or_create(username=status.author.screen_name.lower(), defaults={
  39. 'email': 'user@tweetmarks.de',
  40. 'is_staff': 0,
  41. 'is_active': 1,
  42. 'is_superuser': 0,
  43. 'last_login': datetime.datetime.now(),
  44. 'date_joined': datetime.datetime.now(),
  45. })
  46. if created_user:
  47. twitterprofile, profile_created = Profile.objects.get_or_create(user=db_user, defaults={
  48. 'follow_request_sent': status.author.follow_request_sent,
  49. 'profile_use_background_image': status.author.profile_use_background_image,
  50. 'twitteruser_id': status.author.id,
  51. 'verified': status.author.verified,
  52. 'profile_sidebar_fill_color': status.author.profile_sidebar_fill_color,
  53. 'profile_text_color': status.author.profile_text_color,
  54. 'followers_count': status.author.followers_count,
  55. 'protected': status.author.protected,
  56. 'location': status.author.location,
  57. 'profile_background_color': status.author.profile_background_color,
  58. 'twitteruser_id_str': status.author.id_str,
  59. 'utc_offset': status.author.utc_offset,
  60. 'statuses_count': status.author.statuses_count,
  61. 'description': status.author.description,
  62. 'friends_count': status.author.friends_count,
  63. 'profile_link_color': status.author.profile_link_color,
  64. 'profile_image_url': status.author.profile_image_url,
  65. 'notifications': status.author.notifications,
  66. 'show_all_inline_media': status.author.show_all_inline_media,
  67. 'geo_enabled': status.author.geo_enabled,
  68. 'profile_background_image_url': status.author.profile_background_image_url,
  69. 'screen_name': status.author.screen_name,
  70. 'lang': status.author.lang,
  71. 'following': status.author.following,
  72. 'profile_background_tile': status.author.profile_background_tile,
  73. 'favourites_count': status.author.favourites_count,
  74. 'name': status.author.name,
  75. 'url': status.author.url,
  76. 'created_at': status.author.created_at,
  77. 'contributors_enabled': status.author.contributors_enabled,
  78. 'time_zone': status.author.time_zone,
  79. 'profile_sidebar_border_color': status.author.profile_sidebar_border_color,
  80. 'is_translator': status.author.is_translator,
  81. 'listed_count': status.author.listed_count,
  82. })
  83. print('Profil erstellt')
  84. else:
  85. print('Kein Profil erstellt')
  86. except Exception, e:
  87. print('User Fehler', e)
  88.  
  89. try:
  90. try:
  91. sourceurl = status.source_url
  92. except:
  93. sourceurl = ''
  94. print('no sourceurl')
  95.  
  96. marked_url = tweet_urls[0]
  97.  
  98. tweet, tweet_created = Tweet.objects.get_or_create(tweet_id=status.id, defaults={
  99. 'contributors': status.contributors,
  100. 'truncated': status.truncated,
  101. 'tweet_text': status.text,
  102. 'marked_url': marked_url,
  103. 'in_reply_to_status_id': status.in_reply_to_status_id,
  104. 'tweet_id': status.id,
  105. 'author': db_user,
  106. 'retweeted': status.retweeted,
  107. 'coordinates': status.coordinates,
  108. 'source': status.source,
  109. 'in_reply_to_screen_name': status.in_reply_to_screen_name,
  110. 'id_str': status.id_str,
  111. 'retweet_count': status.retweet_count,
  112. 'in_reply_to_user_id': status.in_reply_to_user_id,
  113. 'favorited': status.favorited,
  114. 'source_url': sourceurl,
  115. 'geo': status.geo,
  116. 'in_reply_to_user_id_str': status.in_reply_to_user_id_str,
  117. 'created_at': status.created_at,
  118. 'in_reply_to_status_id_str': status.in_reply_to_status_id_str,
  119. 'place': status.place,
  120. })
  121. if tweet_created:
  122. print('Tweet erstellt')
  123. except Exception, e:
  124. print('Tweet Fehler', e)
  125. def on_error(self, status_code):
  126. print 'An error has occured! Status code = %s' % status_code
  127. return True # keep stream alive
  128. def on_timeout(self):
  129. print 'Snoozing Zzzzzz'
  130.  
  131.  
  132. def main():
  133. # Prompt for login credentials and setup stream object
  134. username = "mightym"
  135. password = "blabla"
  136. stream = tweepy.Stream(username, password, StreamWatcherListener(), timeout=None)
  137. stream.filter( followers, None )
  138.  
  139.  
  140. try:
  141. main()
  142. except:
  143. sleep(60)
  144. main()
Add Comment
Please, Sign In to add comment