示例#1
0
def load_tweets(data='troll-tweets/tweets.csv'):
    """Load tweets from tweets.csv into database."""

    df = pd.read_csv(data)

    df['user_id'].replace(np.nan, 0, regex=True, inplace=True)
    df['tweet_id'].replace(np.nan, 0, regex=True, inplace=True)
    df.replace(np.nan, '', regex=True, inplace=True)

    #getting rid of columns not in Tweet model.
    df.drop('created_at', axis=1, inplace=True)
    df.drop('expanded_urls', axis=1, inplace=True)
    df.drop('posted', axis=1, inplace=True)
    df.drop('retweeted_status_id', axis=1, inplace=True)
    df.drop('in_reply_to_status_id', axis=1, inplace=True)

    for index, row in df.iterrows():
        user_id, user_key, created_str, retweet_count,\
        retweeted, favorite_count, text, tweet_id, source,\
        hashtags, mentions = row.values.tolist()

        tweet = Tweet(user_id=user_id,
                      user_key=user_key,
                      created_str=created_str,
                      retweet_count=retweet_count,
                      retweeted=retweeted,
                      favorite_count=favorite_count,
                      text=text,
                      tweet_id=tweet_id,
                      source=source,
                      hashtags=hashtags,
                      mentions=mentions)
        db.session.add(tweet)

    db.session.commit()
    def on_data(self, data):
        """
        Called by StreamListener whenever a new tweet arrives
        """
        try:
            tweet_json = json.loads(data)

            coordinates = tweet_json.get('coordinates', {}).get(
                'coordinates', None) if tweet_json.get('coordinates') else None
            place = tweet_json.get('place').get('full_name') if tweet_json.get(
                'place') else ''
            if coordinates is None:
                # If we don't have coordinates we dont care about this tweet
                return True
            tweet = Tweet(id=tweet_json.get('id'),
                          tweet_text=tweet_json.get('text'),
                          user_id=tweet_json.get('user').get('id'),
                          coordinates=coordinates,
                          created_at=tweet_json.get('created_at'),
                          json=tweet_json,
                          last_update=datetime.now(),
                          place=place)
            tweet.save()
        except:
            print_exc()
            db_session.rollback()
        return True
示例#3
0
def create():
    data = request.form
    with db:
        tweet = Tweet(text=data["text"])
        try:
            tweet.save()
        except Exception, e:
            print e
示例#4
0
 def on_post(self, req, resp):
     session = req.env['beaker.session']
     tx = Tweet(**{
         'author_id': session['logged_in'],
         'text': req.params['text']
     })
     db.add(tx)
     db.commit()
     raise falcon.HTTPFound('/')
示例#5
0
def store_tweets():
    for tweet in tweets:
        tweet_id = tweet.id
        tweet_text = clean_tweet(tweet.text)
        tweet_text = unicodedata.normalize('NFKD', tweet_text).encode('ascii','ignore')
        sentiment = tweet_sentiment(tweet_text)
        if(session.query(Tweet).filter_by(tweet_id = tweet_id).scalar() is None):
            newTweet = Tweet(tweet_id=tweet_id, tweet_text= tweet_text, sentiment= sentiment)
            session.add(newTweet)
            session.commit()
示例#6
0
def post_tweet():
    form = TweetForm()
    if form.validate():
        #inseram in baza de date
        tweet = Tweet(user_id=current_user.id,
                      text=form.text.data,
                      date_created=datetime.now())
        db.session.add(tweet)
        db.session.commit()
        # ma intorc pe aceasi pagina
        return redirect(url_for('timeline'))

    return 'Something went wrong'
示例#7
0
def tweet_to_db():
    """Add tweets into db"""

    output = format_tweets()

    text_list = [a.text for a in Tweet.query.all()]

    for tweet in output:
        if tweet[2] not in text_list:  # need to edit this
            tweet = Tweet(handle=tweet[0],
                          time_created=tweet[1],
                          text=tweet[2],
                          retweets=tweet[3])
            db.session.add(tweet)

    db.session.commit()
示例#8
0
def add_tweet():
    if request.method == 'GET':
        return render_template('add_tweet.html')
    else:
        new_text = request.form.get('text')
        new_picture_url = request.form.get('picture_url')
        new_show_location = request.form.get('show_location')
        new_location = request.form.get('location')

        new_tweet = Tweet(text=new_text,
                          picture_url=new_picture_url,
                          show_location=new_show_location,
                          location=new_location)
        session.add(new_tweet)
        session.commit()
        return redirect(url_for('my_feed'))
示例#9
0
def create_tweet(handle, post_or_reply, url, content, comments, views, hearts,
                 last_updated):

    tweet = Tweet(handle=handle,
                  post_or_reply=post_or_reply,
                  url=url,
                  content=content,
                  comments=comments,
                  comment_summary=comment_summary,
                  views=views,
                  hearts=hearts,
                  last_updated=last_updated)

    db.session.add(tweet)

    db.session.commit()

    return tweet
示例#10
0
def save_tweet():
    tweets = get_tweet()
    for tweet in tweets:
        tweet_id = tweet["tweet_id"]
        if session.query(Tweet).filter(Tweet.tweet_id == tweet_id).first():
            break

        tw = Tweet()
        tw.tweet_id = tweet["tweet_id"]
        tw.twitter_id = tweet["twitter_id"]
        tw.twitter_name = tweet["twitter_name"]
        tw.tweeted_at = tweet["datetime"]
        tw.rank_tier = tweet["rank_tier"]
        tw.player_name = tweet["main"]["player_name"]
        tw.character = tweet["main"]["character"]
        tw.rank = tweet["main"]["rank"]
        tw.vsid = tweet["main"]["vsid"]
        tw.comment = tweet["main"]["comment"]
        session.add(tw)
        session.commit()
示例#11
0
def add_tweet():
    if request.method == 'GET':
        return render_template("add_tweet.html")
    else:
        # read form data
        new_text = request.form.get('text')
        new_picture_url = request.form.get('picture_url')
        new_show_location = request.form.get('show_location')
        new_location = request.form.get('location')

        # MISSING CODE HERE FOR UPDATING THE TWEET
        tweet = Tweet()
        tweet.text = new_text
        tweet.picture_url = new_picture_url
        tweet.show_location = new_show_location
        tweet.location = new_location
        session.add(tweet)
        session.commit()

        # redirect user to the page that views all tweets
        return redirect(url_for('my_feed'))
示例#12
0
def save_results():
    """Saving the img and the text from results"""

    # Get form variables
    keyword = request.form["keywords"]
    text_results = request.form["twitter"]
    giphy_url = request.form["giphy"]
    block = request.form["b_text"]
    sentiment = request.form["sentiment"]
    lat = request.form["lat"]
    lng = request.form["long"]

    #Saving current user info into the db
    user_id = session.get("user_id")
    #Saving tweet data to db
    save_twit = Tweet(tweet_text=text_results)
    db.session.add(save_twit)
    db.session.commit()
    #Saving giphy data to db
    save_gif = Picture(giphy_url=giphy_url)
    db.session.add(save_gif)
    db.session.commit()


    new_keyword = Result(keywords=keyword, 
                         tweet_id=save_twit.tweet_id, 
                         giphy_id=save_gif.giphy_id,
                         block_text=block,
                         sentiment=sentiment,
                         generated_at=datetime.datetime.now(),
                         user_id=user_id,
                         lat=lat,
                         lng=lng)

    
    db.session.add(new_keyword)
    db.session.commit()


    return redirect("/users/%s" % user_id)
示例#13
0
def load_tweets():

    i = 0

    while i < 30:
        new_feeling = random.choice(feels)
        print new_feeling
        get_tweets = tApi.get_tweets(query=new_feeling, count=5)
        print get_tweets
        try:
            tweet = random.choice(get_tweets)['text']
        except IndexError:
            continue

        query = Tweet.query.filter_by(tweet_text=tweet).first()

        if query == None:
            tweet_text = Tweet(tweet_text=tweet)
            db.session.add(tweet_text)
            i = i + 1
        else:
            continue

    db.session.commit()