Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # encoding: utf-8
- import datetime
- import re
- from django.core.management import setup_environ
- import settings
- setup_environ(settings)
- from tweetmarks.tweetmark.models import *
- from django.db.models import Q
- from getpass import getpass
- from textwrap import TextWrapper
- import tweepy
- import time
- import urllib
- import BeautifulSoup
- from time import sleep
- CONSUMER_KEY = '6xbOX97j93m6HuNUqaw'
- CONSUMER_SECRET = 'eE6PzQ59O358U8ToTb7PlZSGS5qcQhSLjzSC3fYTy5Y'
- key = '302523037-FgkGWPIT9cnxsREroEnlOGrcjfas7pm3qw93P65q'
- secret = 'lYbRzbWS2kATGxmTHFJPK47X6vc131W2Mt27F5Ko9M'
- auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
- auth.set_access_token(key, secret)
- api = tweepy.API(auth)
- #public_tweets = tweepy.api.public_timeline()
- #for tweet in public_tweets:
- # print tweet.text
- followers = api.followers_ids()
- print(followers)
- class StreamWatcherListener(tweepy.StreamListener):
- def on_status(self, status):
- tweet_urls = re.findall(r"https?://\S+", status.text, re.IGNORECASE)
- if tweet_urls:
- try:
- db_user, created_user = User.objects.get_or_create(username=status.author.screen_name.lower(), defaults={
- 'email': 'user@tweetmarks.de',
- 'is_staff': 0,
- 'is_active': 1,
- 'is_superuser': 0,
- 'last_login': datetime.datetime.now(),
- 'date_joined': datetime.datetime.now(),
- })
- if created_user:
- twitterprofile, profile_created = Profile.objects.get_or_create(user=db_user, defaults={
- 'follow_request_sent': status.author.follow_request_sent,
- 'profile_use_background_image': status.author.profile_use_background_image,
- 'twitteruser_id': status.author.id,
- 'verified': status.author.verified,
- 'profile_sidebar_fill_color': status.author.profile_sidebar_fill_color,
- 'profile_text_color': status.author.profile_text_color,
- 'followers_count': status.author.followers_count,
- 'protected': status.author.protected,
- 'location': status.author.location,
- 'profile_background_color': status.author.profile_background_color,
- 'twitteruser_id_str': status.author.id_str,
- 'utc_offset': status.author.utc_offset,
- 'statuses_count': status.author.statuses_count,
- 'description': status.author.description,
- 'friends_count': status.author.friends_count,
- 'profile_link_color': status.author.profile_link_color,
- 'profile_image_url': status.author.profile_image_url,
- 'notifications': status.author.notifications,
- 'show_all_inline_media': status.author.show_all_inline_media,
- 'geo_enabled': status.author.geo_enabled,
- 'profile_background_image_url': status.author.profile_background_image_url,
- 'screen_name': status.author.screen_name,
- 'lang': status.author.lang,
- 'following': status.author.following,
- 'profile_background_tile': status.author.profile_background_tile,
- 'favourites_count': status.author.favourites_count,
- 'name': status.author.name,
- 'url': status.author.url,
- 'created_at': status.author.created_at,
- 'contributors_enabled': status.author.contributors_enabled,
- 'time_zone': status.author.time_zone,
- 'profile_sidebar_border_color': status.author.profile_sidebar_border_color,
- 'is_translator': status.author.is_translator,
- 'listed_count': status.author.listed_count,
- })
- print('Profil erstellt')
- else:
- print('Kein Profil erstellt')
- except Exception, e:
- print('User Fehler', e)
- try:
- try:
- sourceurl = status.source_url
- except:
- sourceurl = ''
- print('no sourceurl')
- marked_url = tweet_urls[0]
- tweet, tweet_created = Tweet.objects.get_or_create(tweet_id=status.id, defaults={
- 'contributors': status.contributors,
- 'truncated': status.truncated,
- 'tweet_text': status.text,
- 'marked_url': marked_url,
- 'in_reply_to_status_id': status.in_reply_to_status_id,
- 'tweet_id': status.id,
- 'author': db_user,
- 'retweeted': status.retweeted,
- 'coordinates': status.coordinates,
- 'source': status.source,
- 'in_reply_to_screen_name': status.in_reply_to_screen_name,
- 'id_str': status.id_str,
- 'retweet_count': status.retweet_count,
- 'in_reply_to_user_id': status.in_reply_to_user_id,
- 'favorited': status.favorited,
- 'source_url': sourceurl,
- 'geo': status.geo,
- 'in_reply_to_user_id_str': status.in_reply_to_user_id_str,
- 'created_at': status.created_at,
- 'in_reply_to_status_id_str': status.in_reply_to_status_id_str,
- 'place': status.place,
- })
- if tweet_created:
- print('Tweet erstellt')
- except Exception, e:
- print('Tweet Fehler', e)
- def on_error(self, status_code):
- print 'An error has occured! Status code = %s' % status_code
- return True # keep stream alive
- def on_timeout(self):
- print 'Snoozing Zzzzzz'
- def main():
- # Prompt for login credentials and setup stream object
- username = "mightym"
- password = "blabla"
- stream = tweepy.Stream(username, password, StreamWatcherListener(), timeout=None)
- stream.filter( followers, None )
- try:
- main()
- except:
- sleep(60)
- main()
Add Comment
Please, Sign In to add comment