def query_twitter_api(): """Query Elon's twitter for references to keywords.""" print("Query Initiating...") auth = tweepy.OAuthHandler( CONSUMER_KEY, CONSUMER_SECRET_KEY ) auth.set_access_token( KEY, SECRET_KEY ) api = tweepy.API(auth) twitter_status = api.user_timeline( id="elonmusk", count=1, tweet_mode="extended" )[0]._json tweet_text = twitter_status["full_text"] if re.search(r'\bbitcoin\b', tweet_text): obj = { "twitter_handle": "elonmusk", "created_at": twitter_status["created_at"], "text": tweet_text } print("Tweet mentioning bitcoin found") tweet = Tweet(**obj) tweet.save() else: print("No tweet mentioning bitcoin found")
def post_tweet_view(request): if request.user.is_authenticated: current_user = request.user notes = len( Notification.objects.filter(notify_user=current_user).filter( is_seen=False)) else: notes = 0 if request.method == "POST": form = PostTweetForm(request.POST) if form.is_valid(): data = form.cleaned_data new_tweet = Tweet( user_tweeted=current_user, content=data.get('content'), #date_and_time=data.get('date_and_time') ) new_tweet.save() # Either gets after "@" or returns false notify = read_reggie(data.get('content')) if notify: # Gets user being "@" @ by the username in the mention #note_user = MyTwitterUser.objects.get(username=notify) for name in notify: Notification.objects.create( # Attributes user notify_user=MyTwitterUser.objects.get(username=name), # Gets tweet obj tweet=new_tweet, is_seen=False) return HttpResponseRedirect(reverse('home')) form = PostTweetForm() return render(request, 'tweet_form.html', {'form': form, 'notes': notes})
def twitter(request): if 'currentUser' in request.session: currentUser = request.session['currentUser'] myTweets = Tweet.objects.filter(username=currentUser) theirTweetList = [] getFollowedUsers = userFollowing.objects.filter(username=currentUser) for f in getFollowedUsers: followedTweets = Tweet.objects.filter(username=f.userFollowed) for t in followedTweets: theirTweetList.insert(0, t) if 'searchUser' in request.POST: searchedUser = request.POST['searchUser'] request.session['searchedUser'] = searchedUser return HttpResponseRedirect("/usersearch/") if 'incomingTweet' in request.POST: newTweet = request.POST['incomingTweet'] #process tweet_post, then save in database if len(newTweet) <= 140 and len(newTweet) > 1: addTweet = Tweet(message=newTweet,username=currentUser) addTweet.save() #add new tweet return render(request, 'twitter.html', {'result': "", 'currentUser': currentUser, 'myTweets':myTweets, 'theirTweets': theirTweetList}, context_instance=RequestContext(request)) else: return render(request, 'twitter.html', {'result': "Your tweet needs to be between 1 to 140 characters.", 'currentUser': currentUser, 'myTweets': myTweets, 'theirTweets': theirTweetList}, context_instance=RequestContext(request)) else: return render(request, 'twitter.html', {'result': "", 'currentUser': currentUser, 'myTweets':myTweets, 'theirTweets': theirTweetList}, context_instance=RequestContext(request)) else: return HttpResponseRedirect("/")
def tweetform_view(request): html = "tweetform.html" if request.method == "POST": form = TweetForm(request.POST) if form.is_valid(): data = form.cleaned_data newTweet = Tweet(tweetbody=data['tweetbody'], date_filed=data['date_filed'], author=request.user) newTweet.save() # This newTweet['tweetbody'] is reaching into db # if '@' in newTweet['tweetbody']: pattern = re.search("@[\w\d]+", newTweet.tweetbody) if pattern is not None: pattern = pattern.group(0)[1:] target_user = TwitterUser.objects.get(username=pattern) Notification.objects.create( target_user=target_user, tweet=newTweet, ) baseProfile = TwitterUser.objects.get(username=request.user) newTweet.twittuser.add(baseProfile) return HttpResponseRedirect(reverse("homepage")) form = TweetForm() return render(request, html, {"form": form})
def make_tweet(request): if not request.user.is_authenticated: return HttpResponseRedirect(reverse('auth:log_in')) if request.POST: tweet = Tweet(title=request.POST['title'], content=request.POST['content'], user=request.user) tweet.save() return HttpResponseRedirect( reverse('tweet:show_tweet', args=(tweet.id, ))) return render(request, 'tweet/make_tweet.html')
def post(self, request, username): form = TweetForm(request.POST) if form.is_valid(): user = User.objects.get(username=username) tweet = Tweet(text=form.cleaned_data['text'], user=user, country=form.cleaned_data['country']) tweet.save() words = form.cleaned_data['text'].split(" ") for word in words: if word[0] == "#": hashtag, created = HashTag.objects.get_or_create(name=word[1:]) hashtag.tweet.add(tweet) return HttpResponseRedirect('/user/'+username)
def new_tweet_view(request): if request.method == 'POST': form = NewTweetForm(request.POST) if form.is_valid(): content = form.cleaned_data['content'] new_tweet = Tweet( content=content, composer=request.user ) new_tweet.save() find_mention_notifications(new_tweet) return HttpResponseRedirect( reverse('homepage') )
def crawl_tweets_for(username, since): for tweet in all_tweets_since(username, since): try: tweet_record = Tweet.objects.get(pk=tweet["id"]) tweet_record.retweets = tweet["retweet_count"] except Tweet.DoesNotExist: tweet_data = { "tweet_id": tweet["id"], "text": tweet["text"], "created_at": tweet["created_at"], "retweets": tweet["retweet_count"], "username": UserTweet.objects.get(username=username), } tweet_record = Tweet(**tweet_data) tweet_record.save()
def v2_tweet(request): token_hash = request.GET.get('key') if not token_hash: return HttpResponse('you did not send any key') try: token = TokenV2.objects.get(key=token_hash) except TokenV2.DoesNotExist: return HttpResponse('invalid key!') user = token.user # make tweet with request.GET and user and save it title = request.GET.get('title') content = request.GET.get('content') tweet = Tweet(title=title, content=content, user=user) tweet.save() return HttpResponse(tweet.title + " -- was created successfully ")
def save_tweets(self, public_tweets): for tweet in public_tweets: User.from_tweepy(tweet.author) for u in tweet.entities['user_mentions']: User.from_dict(u, api.get_user) t = Tweet.from_tweepy(tweet) print t.text.encode('utf8')
def post(self, request): try: data = json.loads(request.body) tweet = Tweet(user_id=data["user_id"], user_content=data["user_content"]) tweet.save() getpk = Tweet.objects.filter(user_id=data["user_id"]).last().pk return JsonResponse( { "content_id": f"{getpk}", "message": "Tweet Success" }, safe=False, status=200) except KeyError: return JsonResponse({"message": "Tweet Failed"}, safe=False, status=200)
def saveresult(result,key,valeur): try : auteur = Auteur.objects.get(Nom=result['from_user_name']) except : auteur = Auteur(Nom=result['from_user_name']) auteur.save() hashtags = extract_hash_tags(result["text"]) tnom = Tweet(Auteur=auteur, Contenu=result["text"],TypeRecherche=key,Valeur=valeur) tnom.save() for h in hashtags : try : h1 = HashTag.objects.get(HashTag='#%s'%h) except : h1 = HashTag(HashTag='#%s'%h) h1.save() tnom.HashTag.add(h1) tnom.save()
def add_tweet(request): """添加推文""" if request.method == 'POST': title = request.POST.get('title') content = request.POST.get('content') user_id = request.POST.get('user_id') t = Tweet() t.title = title t.content = content t.user_id = user_id t.read_num = gen_random_num() t.save() return JsonResponse({'code': 200, 'msg': 'success'})
def save_tweets(raw_tweets): tweets_author = raw_tweets[0]['user'] tweets_author_id = tweets_author['id'] author_kwargs = { 'author_id': tweets_author_id, 'screen_name': tweets_author.get('screen_name'), 'author_name': tweets_author.get('name'), # Stupid datetime...need to figure out how to parse # 'author_created': tweets_author.get('created_at'), 'author_description': tweets_author.get('description') } # TO-DO: Figure out get_or_create on a variable but save with **kwargs try: author = Author.objects.get(author_id=tweets_author_id) except Author.DoesNotExist: author = Author(**author_kwargs) author.save() for tweet in raw_tweets: tweet_id = tweet['id'] tweet_kwargs = { 'tweet_id': tweet_id, 'author': author, 'content': tweet['text'], # date_published: tweet['created_at'], 'date_published': datetime.datetime.now(), 'coordinates': tweet.get('coordinates'), # Hashtags and user_mentinos TO BE IMPLEMENTED # 'user_mentions': tweet['entities'].get('user_mentions'), # hashtags: TO BE IMPLEMENTED } try: our_tweet = Tweet.objects.get(tweet_id=tweet_id) except Tweet.DoesNotExist: our_tweet = Tweet(**tweet_kwargs) our_tweet.save() return
def create_tweet(request): if request.method=='POST': message=request.POST['message'] profile_obj=Profile.objects.get(user=request.user) t_obj=Tweet() t_obj.profile=profile_obj t_obj.message=message t_obj.save() return HttpResponseRedirect(reverse('index')) else: form=CreateTweet() return render(request,'tweet/create_tweet.html',{'form':form})
def feed(request): if request.method == 'POST': form = TweetForm(request.POST, request.FILES) if form.is_valid(): tweet = Tweet() tweet.content = form.cleaned_data['tweet'] tweet.image = form.cleaned_data.get('image') tweet.user = request.user tweet.save() form = TweetForm() else: form = TweetForm() tweets = Tweet.objects.order_by('-post_date') context = { 'tweets': tweets, 'form': form, 'user': request.user, } return render(request, "tweet/feed.html", context)
def post(self,request): tweet_input = json.loads(request.body) user_id = tweet_input['user_id'] tweet_content = tweet_input['tweet'] Tweet(tweet = tweet_content, user_id = User.objects.get(id=user_id)).save() return JsonResponse({ 'tweet_content': tweet_content })
def get_queryset(self): #expressão de busca enviada expression = self.request.query_params.get('search') if (expression): #codifica a expressão para o padrão utilizado na web search_word = requests.utils.quote(expression) API_KEY = '5MU3Vu3zXm6xn0XQuIZzg2Q1I' API_SECRET = 'huaphtigFjD0riKGuT9rZHig6RCCr0zR7eeBsNvZtldcXCYnQz' ACCESS_TOKEN = '27258579-r6APNk6Y1mracJWcdAln3C6NsC5hj9dZKG8G2f654' ACCESS_TOKEN_SECRET = 'ulF8lFGppaeqEoA7TS17IGAnCMYjsI8QinyGLTI8uCQHP' session = OAuth1Session(API_KEY, API_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET) url = "https://api.twitter.com/1.1/search/tweets.json?lang=pt&q=" + search_word + "&count=30&tweet_mode=extended" response = session.get(url) tweets = json.loads(response.content.decode('utf-8')) #armazena os dados dos usuarios user_data = [] #armazena os tweets dos usuários para posterior classificação user_tweets = [] for t in tweets['statuses']: #converte a data para o timezone do Brasil d = datetime.strptime( t['created_at'], '%a %b %d %H:%M:%S %z %Y').replace( tzinfo=timezone.utc).astimezone( pytz.timezone('America/Sao_Paulo')).strftime( '%Y-%m-%d %H:%M:%S') data = { "data": d, "username": t['user']['screen_name'], "tweet_id": t['id'], "tweet": t['full_text'] } user_data.append(data) user_tweets.append(t['full_text']) #carrega o modelo gravado em arquivo text_clf = pickle.load( open(settings.MEDIA_ROOT + "/model.sav", 'rb')) #classifica os tweets predicted = text_clf.predict(user_tweets) #salva o termo de busca search = Search() search.expression = expression search.save() for x in range(len(predicted)): user_model = Tweet() user_model.user = user_data[x]['username'] user_model.tweet_date = user_data[x]['data'] user_model.tweet_id = user_data[x]['tweet_id'] user_model.tweet_text = user_data[x]['tweet'] user_model.classification = predicted[x] user_model.search = search user_model.save() return Tweet.objects.filter(search=search) elif (self.request.query_params.get('search_id')): search_id = self.request.query_params.get('search_id') return Tweet.objects.filter(search=search_id) else: return Tweet.objects.all()
def crawl(request, username=None): Tweet.crawl_for(username) return HttpResponse("OK")
def crawl(request, username=None): """Given some twitter username get all the tweets since some specific date""" Tweet.crawl_for(username) return HttpResponse("OK")
def create_tweet(user_id, mutter): tweet = Tweet(tweeted_by_id=user_id, mutter=mutter) tweet.save() return tweet