示例#1
0
 def get(self, user_id):
     today_date = datetime.now(tz=timezone("US/Eastern")).date()
     tweets = TweetModel.find_recent_week(user_id, today_date)
     user = UserModel.find_by_user_id(user_id)
     screen_name = user.screenName
     res = get_html(screen_name, tweets)
     return res['body'], res['code']
示例#2
0
    def get(self, user_id):
        today_date = datetime.now(tz=timezone("US/Eastern")).date()
        tweets = TweetModel.find_recent_week_no_rt(user_id, today_date)
        #tweets.sort(key = lambda t : t.created_at)
        res = pull_up(tweets, today_date)

        return res['body'], res['code']
示例#3
0
    def get(self, user_id):

        today_date = datetime.now(tz=timezone("US/Eastern")).date()
        tweets = TweetModel.find_recent_week(user_id, today_date)
        res = pull_up(tweets, today_date)

        return res['body'], res['code']
示例#4
0
    def get(self, tweet_id):

        tweet = TweetModel.find_by_tweet_id(tweet_id)
        ''' inspect module
        columns = [m.key for m in user.__table__.columns]
        print(columns)
        '''

        if tweet:
            return {"tweet": tweet.json()}, 200
        else:
            return {"message": "User not found in the databse"}, 400
示例#5
0
    def post(self):
        data = self.parser.parse_args()
        if not data['user_id']:
            return {'message': 'User ID cannot be blank.'}, 400

        if not data['since_id']:
            return {'message': 'Since ID cannot be blank.'}, 400

        id = int(data['user_id'])
        since_id = int(data['since_id']) - 1
        time_now = datetime.now(tz=timezone("US/Eastern"))
        user = UserModel.find_by_user_id(id)

        within_five = not user.lastUpdatedTime or timezone(
            'US/Eastern').localize(
                user.lastUpdatedTime) >= time_now - timedelta(minutes=1)

        if within_five:
            return {'message': 'Already updated in the last five minutes'}, 200
        else:
            user.lastUpdatedTime = time_now
            user.save_to_db()

        new_tweets = Tweetify.update_list(id, since_id)
        pre_tweets = TweetModel.find_tweets_before(id, since_id)

        try:
            #print('length is {}'.format(len(new_tweets)))
            for t in pre_tweets:

                if not new_tweets:
                    break

                t_id = t.id
                '''
                setattr(t, "rt_count", new_tweets[t_id]['rt'])
                setattr(t, "fv_count", new_tweets[t_id]['fav'])
                '''
                #print('before, {} rt is {}, fv is {}'.format(t.id, t.rt_count, t.fv_count))
                t.rt_count = new_tweets[t_id]['rt']
                t.fv_count = new_tweets[t_id]['fav']
                #print('after, {} rt is {}, fv is {}'.format(t.id, t.rt_count, t.fv_count))
                t.save_to_db()

            message = "updated {} tweets.".format(pre_tweets.count())
            return {'message': message}, 200
        except:
            traceback.print_exc()
            return {'message': 'update failed'}, 500
示例#6
0
def get_single_html(screen_name, t_id):
    '''
        could save one parameter, using just t_id => but that would cost
        one extra Twitter API call
    '''
    tweet_url = "https://twitter.com/{}/status/{}".format(screen_name, t_id)
    request_url = "https://publish.twitter.com/oembed?url=" + tweet_url
    r = requests.get(request_url)
    try:
        html_string = r.json()['html']
    except:
        html_string = '<h2>This tweet does not exist any longer</h2>'
        tweet = TweetModel.find_by_tweet_id(t_id)
        if tweet:
            html_string += '\n <h3>this is what we pull from the database:</h3> \n {}'.format(
                tweet.full_text)

    return html_string
示例#7
0
    def get(self, screen_name):
        # TODO check if user already in database and change endtime accordling
        time_now = datetime.now(tz=timezone("US/Eastern"))
        user = UserModel.find_by_screen_name(screen_name)
        try:
            within_a_week = timezone('US/Eastern').localize(
                user.lastCheckedTime).date() >= time_now.date() - timedelta(
                    days=6)
        except:
            within_a_week = False

        if user and within_a_week:
            end_date = user.lastCheckedTime
            since_id = user.lastTweetId
        else:
            end_date = time_now - (timedelta(days=6))
            since_id = "-1"

        tweet_user = Tweetify(screen_name, end_date, since_id, time_now)

        data = {
            "userId": tweet_user.user_id,
            "screenName": screen_name,
            "timestamp": time_now,
        }

        result = tweet_user.result

        # Not user by this screen_name
        if result["error"]:
            data["isSuccess"] = False
            data["statusCount"] = 0
            search = SearchModel(**data)
            search.save_to_db()
            return {
                "search_info": search.json(),
                "error": result["error"]
            }, 404

        # Save to user db
        user_data = result["user"][0]

        if user:
            user.name = user_data["name"]
            user.insensitive_name = user_data["insensitiveName"]
            user.description = user_data["description"]
            user.followers = user_data["followers"]
            user.friends = user_data["friends"]
            user.statusCount = user_data["statusCount"]
            user.favCount = user_data["favCount"]
            user.location = user_data["location"]
            user.isVerified = user_data["isVerified"]
            user.avatarUrl = user_data["avatarUrl"]
            user.bannerUrl = user_data["bannerUrl"]
            user.lastTweetId = user_data["lastTweetId"]
            user.lastCheckedTime = user_data["lastCheckedTime"]
            user.webUrl = user_data["webUrl"]
            user.tweets_onfile += len(result["tweets"])
            user.save_to_db()

        else:
            new_user = UserModel(tweets_onfile=len(result["tweets"]),
                                 **user_data)
            new_user.save_to_db()

        # TODO Save to tweets db
        tweets = result["tweets"]

        for t in tweets:
            tweet_data = {
                "id": t.id_str,
                "created_at": t.created_at,
                "user_id": tweet_user.user_id,
                "user_screen_name": t.author.screen_name,
                "full_text": t.full_text,
                "is_quote_status": t.is_quote_status,
                "rt_count": t.retweet_count,
                "fv_count": t.favorite_count,
            }
            try:
                tweet_data["retweeted_statsus_id"] = t.retweeted_status.id
                tweet_data["is_retweeted"] = True
                tweet_data["fv_count"] = t.retweeted_status.favorite_count
            except:
                tweet_data["is_retweeted"] = False
                tweet_data["retweeted_statsus_id"] = None

            new_tweet = TweetModel(**tweet_data)
            new_tweet.save_to_db()

        # Save to DB & return flag to front-end
        data["isSuccess"] = True
        data["statusCount"] = len(tweets)
        search = SearchModel(**data)
        search.save_to_db()

        return {"search_info": search.json()}, 200
示例#8
0
def pull_up(tweets, today_date):
    count = tweets.count()
    if count == 0:
        return {
            'body': {
                'message': 'No recent tweets found',
            },
            'code': 404,
        }
    res = {}
    res['tweets'] = []
    res['total_fav'] = 0
    res['total_rt'] = 0
    res['count'] = count
    #res['html_string'] = []

    days = 0
    byDay = {}
    interacts = []
    while days < 7:
        this_date = today_date - timedelta(days=days)
        datestr = this_date.isoformat()
        byDay[datestr] = 0
        interacts.append({
            'date': datestr,
            'favs': 0,
            'rts': 0,
        })
        days += 1

    hours = 0
    hour_freq = {}
    while hours < 24:
        hour_freq[hours] = 0
        hours += 1

    max_rt = -1
    max_fv = -1
    max_rt_id = -1
    max_fv_id = -1

    datePtr = 6
    for t in tweets:
        while t.created_at.date().isoformat() != interacts[datePtr]['date']:
            datePtr -= 1

        interacts[datePtr]['favs'] += t.fv_count
        interacts[datePtr]['rts'] += t.rt_count

        if t.rt_count > max_rt:
            max_rt = t.rt_count
            max_rt_id = t.id

        if t.fv_count > max_fv:
            max_fv = t.fv_count
            max_fv_id = t.id

        max_fv = t.fv_count if t.fv_count > max_fv else max_fv
        datestr = t.created_at.date().isoformat()

        hour = t.created_at.hour
        hour_freq[hour] += 1
        this_tweet = {
            "id": t.id,
            "favs": t.fv_count,
            "rts": t.rt_count,
        }
        res['tweets'].append(this_tweet)
        res['total_fav'] += t.fv_count
        res['total_rt'] += t.rt_count
        byDay[datestr] += 1

    time_freq = {
        'early_morning': 0,
        'morning': 0,
        'afternoon': 0,
        'night': 0,
    }
    for h in range(0, 7):
        time_freq['early_morning'] += hour_freq[h]
    for h in range(7, 12):
        time_freq['morning'] += hour_freq[h]
    for h in range(12, 19):
        time_freq['afternoon'] += hour_freq[h]
    for h in range(19, 24):
        time_freq['night'] += hour_freq[h]

    res['time_freq'] = time_freq
    res['hour_freq'] = hour_freq
    res['avg_rt'] = res['total_rt'] / count
    res['avg_fav'] = res['total_fav'] / count
    res['max_fv'] = {
        'count': max_fv,
        'id': max_fv_id,
        'text': TweetModel.find_by_tweet_id(max_fv_id).full_text,
    }
    res['max_rt'] = {
        'count': max_rt,
        'id': max_rt_id,
        'text': TweetModel.find_by_tweet_id(max_rt_id).full_text,
    }

    res['interacts'] = interacts
    res['byDay'] = byDay
    return {
        'body': res,
        'code': 200,
    }