Created
May 21, 2015 10:35
-
-
Save RyuaNerin/9a4f0ef5ba530ce654d3 to your computer and use it in GitHub Desktop.
어제 트윗 분석
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# -*- coding:utf-8 -*- | |
# 이 사람은 오늘도 열심히 트위터를 하였다 | |
from twython import Twython | |
from datetime import datetime, timedelta | |
import io | |
import math | |
api = Twython( | |
'App Key', | |
'App Secret', | |
'User Token', | |
'User Secret') | |
if __name__ == '__main__': | |
now = datetime.now() | |
now = datetime(now.year, now.month, now.day) - timedelta(days = 1) | |
max_id = 0 | |
max_idn = 0 | |
tweets = 0 | |
mentions = 0 | |
retweets = 0 | |
hours = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] | |
getTweets = True | |
while getTweets: | |
tws = api.get_user_timeline(count = 200) if max_id == 0 else \ | |
api.get_user_timeline(count = 200, max_id = max_id) | |
max_id = int(tws[len(tws) - 1]['id']) | |
for tw in tws: | |
if int(tw['id']) == max_idn: | |
continue | |
date = datetime.strptime(tw['created_at'], '%a %b %d %H:%M:%S +0000 %Y') | |
date = date + timedelta(hours = 9) | |
print(datetime.strftime(date, '%Y-%m-%d %H:%M:%S')) | |
if date < now: | |
getTweets = False | |
break | |
if date.year == now.year and date.month == now.month and date.day == now.day : | |
hours[date.hour] += 1 | |
tweets += 1 | |
if 'retweeted_status' in tw: | |
retweets += 1 | |
else: | |
try: | |
if len(tw['entities']['user_mentions']) > 0: | |
mentions += 1 | |
except: | |
pass | |
max_idn = max_id | |
hourMax = 0 | |
for hour in range(0, 23): | |
if hours[hour] > hours[hourMax]: | |
hourMax = hour | |
cycle = 86400 / tweets | |
if cycle > 3600: | |
cycle = '%d시 %d분 %d초' % (cycle // 3600, cycle // 60 % 60, cycle % 60) | |
elif cycle > 60: | |
cycle = '%d분 %d초' % (cycle // 60 % 60, cycle % 60) | |
else: | |
cycle = '%d초' % (cycle % 60) | |
api.update_status( | |
status = ''' | |
%04d-%02d-%02d | |
트윗 : %d | |
알티 : %d | |
멘션 : %s | |
주기 : %s | |
주 시간대 : %02d:00 ~ %02d:00 | |
어제도 훌륭하게 트위터를 했다. | |
''' % (now.year, now.month, now.day, tweets, retweets, mentions, cycle, hourMax, hourMax + 1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment