Roughly calculate the estimated sleep time based on twitter usage time

Yes. If you look for it, I think it's in a doko like this, but somehow I wanted to do it myself.

Assumed environment and usage Calculate based on the time you received by throwing good morning / good night with @ from your main twitter account to the chore bot account.

jikan.py



#!/usr/bin/env python
# -*- coding:UTF-8 -*-

from urllib import urlencode
import re
import random
import datetime
import tweepy
import oat #I store OAuth tokens for my bot function

jikan = datetime.datetime.now()
#It ’s about this, balancing it with the moving interval.
#I wonder if there are any omissions.
tl = oat.api.mentions_timeline()
for i in range(1,5):
    tll=tl[i].text
    if re.search(u'@xxx Good night', tll):
        jk =  tl[i].created_at
#        jk = jikan.strftime('%Y/%m/%d %H:%M:%S')
        t = open('/kanri.txt', 'w')
        t.write(str(jk))
        t.close()
#        tane = 'Thank you for your hard work. Good night{0:02d}Time{0:02d}I set it in minutes.'.format(jikan.hour, jikan.minute)
        tane = 'Thank you for your hard work. Good night%s time%I set it in about s minutes.' % (str(jk.hour+9), str(jk.minute))
        i+=5
    elif re.search(u'@xxx Good morning', tll):
        f = open('/kanri.txt', 'r')
        line = f.read()
        jk = tl[i].created_at
        sabun = jk- datetime.datetime.strptime(line, "%Y-%m-%d %H:%M:%S") 
        f.close()
        tane = 'Good morning. Estimated sleep time from twitter usage difference%It's about s.' % str(sabun)
        break
    else: pass

oat.client.request('https://api.twitter.com/1.1/statuses/update.json', 'POST', urlencode({'status': tane}))

Yes, the variable names are also appropriate and everything is messy. I tried to adjust the difference in estimated sleep time, but it didn't work, so I gave up. I can use the difference between days in days and the difference in seconds in seconds immediately, but I can only think of whether to calculate hour and minute separately or cast them with str and remove them with formatting or regular expressions. .. Since the purpose is only to calculate the time, I plan to make corrections while using it for the time being.

About prevention of the same post http://qiita.com/clarinet758/items/2a2f2ed176a5b25ae0d2

2013/11/28 Modified version including prevention of the same post

op.py


#!/usr/bin/env python
# -*- coding:UTF-8 -*-
import urllib2
import lxml.html
from urllib import urlencode
import re
import random
import oat
import datetime
import tweepy
import json

y = datetime.datetime.now()
tll = oat.api.mentions_timeline()

## word
aut=u'My screen name'
bot=u'Bot screen name'
oya=re.compile(u'Good night')
otu=re.compile(u'Thank you for your hard work')
you=re.compile(u'Good morning')
oha=re.compile(u'Usage difference')
kanri='/xxx/kanri.txt'
## template
neru = u'@%s @%s Thank you for your hard work. Bed in%s time%I set it in about s minutes.'
kisy = u'@%s @%s Good morning! good morning! are u there? Mabushi! Mabushi! Estimated sleep time from twitter usage difference%It's about s.'

def suimin():
    i=0
    while i < 4:
        tex = tll[i].text
        men = tll[i].author.screen_name

#Good night mode
#If I say good night, it's a temporary action. Say thank you for your hard work or say it and exit.
        if oya.search(tex) and men == aut:
#When good night hits, skip the posting time and write to the management file
#It's useless because I rewrite the time every time I sleep, so I will improve it from the next time onwards
#Originally here
            jk = tll[i].created_at

            jkhour=jk.hour+9
            if jkhour>23: jkhour=jkhour-24
            tane = neru % (aut, bot, str(jkhour), str(jk.minute))
#When good night hits, go looking for new arrivals
            m=i
            while m>=0:
                chk=tll[m].text
                if otu.search(chk):
                    break
                else:
                    m,i = m-1,i-1
#If you go to see new arrivals from good night and you are not tired, i-Should be 1
            if i<0:
                t = open(kanri, 'w')
                t.write(str(jk))
                t.close()
                jkhour=jk.hour+9
                if jkhour>23: jkhour-=24
###I also threw the tweet method to the OAuth token
#                oat.client.request('https://api.twitter.com/1.1/statuses/update.json', 'POST', urlencode({'status': tane.encode('utf-8')}))
                message = tane.encode('utf-8')
                oat.tweet(message)
            else:
                break
#Add i to forcibly end the search.
            i=10

#Good morning
        elif you.search(tex) and men == aut:
            f = open(kanri, 'r')
            line = f.read()
#Good morning, omit the posting time and calculate the difference time from the good night log
            jk = tll[i].created_at
            sabun = jk- datetime.datetime.strptime(line, "%Y-%m-%d %H:%M:%S") 
            f.close()
            tane = kisy % (aut, bot, str(sabun))
            m=i
            while m>=0:
#Exit when the usage difference hits
                chk=tll[m].text
                if oha.search(chk):
                    break
                else:
                    m,i = m-1,i-1
            if i<0:
                message = tane.encode('utf-8')
                oat.tweet(message)
            else:
                break
            i=10
        else:
#            print 'no hit'
#If you don't have good night or good morning, add i and go to see old posts
            i+=1

In the past, we have dealt with a problem that is repeated in a day. The instruction to tweet was thrown into the same file together with OAuth token management. Now, even if the URL to tweet changes due to API changes, there is no need to rewrite it here and there. The variable names are still dirty. I tried to make people say 3 o'clock instead of 27:00.

Recommended Posts

Roughly calculate the estimated sleep time based on twitter usage time
Post the subject of Gmail on twitter
Rename based on file modification time (Linux)