-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.py
53 lines (49 loc) · 1.94 KB
/
post.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import tweepy
import pymysql
from dotenv import load_dotenv
import os
from os.path import join,dirname
from datetime import datetime, timezone, timedelta
dotenv_path=join(dirname(__file__),'.env')
load_dotenv(dotenv_path)
MYSQL_HOST=os.environ.get("MYSQL_HOST")
MYSQL_USER=os.environ.get("MYSQL_USER")
MYSQL_PASSWORD=os.environ.get("MYSQL_PASSWORD")
MYSQL_DATABASE=os.environ.get("MYSQL_DATABASE")
API_Key=os.environ.get("API_Key")
API_Key_Secret=os.environ.get("API_Key_Secret")
Access_Token=os.environ.get("Access_Token")
Access_Token_Secret=os.environ.get("Access_Token_Secret")
def make_connection():
return pymysql.connect(host=MYSQL_HOST,
user=MYSQL_USER,
password=MYSQL_PASSWORD,
db=MYSQL_DATABASE,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
auth = tweepy.OAuthHandler(API_Key, API_Key_Secret)
auth.set_access_token(Access_Token, Access_Token_Secret)
api = tweepy.API(auth)
conn = make_connection()
with conn.cursor() as cursor:
sql = "SELECT * FROM contest_info"
cursor.execute(sql)
for tmp in cursor:
day, _, time, _, _ = tmp["schedule"].split()
start_time = datetime.strptime(day + " " + time, "%Y-%m-%d %H:%M")
cur_jp = datetime.now() + timedelta(hours=9)
if cur_jp < start_time and cur_jp + timedelta(minutes=90) > start_time:
res = []
res.append(tmp["title"])
res.append(tmp["schedule"])
res.append(tmp["rated"])
res.append("writer: " + tmp["writer"])
res.append("tester: " + tmp["tester"])
res.append(tmp["url"])
api.update_status("\n".join(res))
sql = "DELETE FROM contest_info WHERE title = %s"
cursor.execute(sql, (tmp["title"]))
elif cur_jp > start_time:
sql = "DELETE FROM contest_info WHERE title = %s"
cursor.execute(sql, (tmp["title"]))
conn.commit()