Brennan-M / TwitterFeels

See how U.S. states "Feel" about a given topic using Tweets
1 stars 1 forks source link

to make twitter interactions easier. #7

Open d-kz opened 9 years ago

d-kz commented 9 years ago

!/usr/bin/python

from tweepy import Stream from tweepy import OAuthHandler from tweepy.streaming import StreamListener import pymongo from pymongo import MongoClient import datetime

import json

!! Things you care about, Denis

client = MongoClient() ##!! db = client['tweetdb'] ##!! collection = db['tweets'] ##!!

ckey = '9BISaQTrryl5V9wIOv56g' csecret = 'aJeiFLbIiYFk8sb4TVsraw6zqjoEParQcrcSpZzY6U' atoken = '2295205662-jOLHa9LbYXzsTpP61FXEVeRInpT6wVe01nK6HrW' asecret = '0UtCBtu59c8wwHSJeyAuHCs7agGeSFccKRth1FhrlVt8G'

class listener(StreamListener):

def on_data(self,data):
    try:
        tweet = json.loads(data)
        tweet_id = tweet["id"]
        if tweet["lang"] != "en":
            pass

        ##!! Checking to see if the tweet is already in the database
        if collection.find({"tweet_id": tweet_id}).limit(1).count() == 0:

            text = tweet["text"]
            #just date
            today = datetime.date.today()
            date = datetime.datetime.strptime(str(today), '%Y-%m-%d')
            tweet_coord = tweet["coordinates"]

            ##!! Setting up object to put into the database
            my_tweet = {"tweet_id": tweet_id,
                        "text": text,
                        "date": date,
                        "coordinates": tweet_coord
                        }

            my_tweet["text"]=tweet["text"].encode('ascii','ignore')
            my_tweet["text"]=tweet["text"].replace('"','')

            ##!! Inserting the tweet into the mongo database                
            this_tweet_id = collection.insert(my_tweet)

            if tweet_coord != None:
                print my_tweet
        else:
            print "Error"
        return True

    except:
        pass

    return True

def on_error(self,status):
    print status

auth = OAuthHandler(ckey,csecret) auth.set_access_token(atoken, asecret) twitterStream = Stream(auth, listener()) common_filter = ['@','is','a','are','if','the','I','in','and'] twitterStream.filter(track=common_filter)

d-kz commented 9 years ago

Here is some code that reads in tweets that makes the whole interaction smoother. "tweepy" does authorization stuff for us. We can probably find something similar for JS. Just for future reference

Brennan-M commented 9 years ago

What do you mean reads in tweets? Doesnt our program already do this?

d-kz commented 9 years ago

oh yeah, I just mean that we can avoid the weird interaction with twitter authorization and let some module handle it for us to make things smoother.

Brennan-M commented 9 years ago

I mean we could write a short script to make us stop having to authorize it too, but yeah. I think I will look into hosting it soon

d-kz commented 9 years ago

Ok sweet. let me know when you will! I want to get some more insight into marketing and how we could directly address the need with this.