Skip to content

Commit ef791eb

Browse files
committed
against changing display
1 parent 4059338 commit ef791eb

File tree

5 files changed

+113
-109
lines changed

5 files changed

+113
-109
lines changed

.gitignore

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# local env files
2-
.env
3-
.env.local
1+
# local env files
2+
.env
3+
.env.local
44
.env.*.local

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# OnlineMathContestNotificationBot
2-
OnlineMathContestのコンテスト開始を通知します。
1+
# OnlineMathContestNotificationBot
2+
OnlineMathContestのコンテスト開始を通知します。

post.py

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

requirements.txt

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
beautifulsoup4==4.10.0
2-
PyMySQL==1.0.2
3-
requests==2.27
4-
requests-oauthlib==1.3.0
5-
python-dotenv==0.17.1
1+
beautifulsoup4==4.10.0
2+
PyMySQL==1.0.2
3+
requests==2.27
4+
requests-oauthlib==1.3.0
5+
python-dotenv==0.17.1
66
tweepy==4.8.0

scrape.py

+51-48
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,52 @@
1-
import requests
2-
from bs4 import BeautifulSoup
3-
import pymysql
4-
from dotenv import load_dotenv
5-
import os
6-
from os.path import join,dirname
7-
8-
dotenv_path=join(dirname(__file__),'.env')
9-
load_dotenv(dotenv_path)
10-
MYSQL_HOST=os.environ.get("MYSQL_HOST")
11-
MYSQL_USER=os.environ.get("MYSQL_USER")
12-
MYSQL_PASSWORD=os.environ.get("MYSQL_PASSWORD")
13-
MYSQL_DATABASE=os.environ.get("MYSQL_DATABASE")
14-
def make_connection():
15-
return pymysql.connect(host=MYSQL_HOST,
16-
user=MYSQL_USER,
17-
password=MYSQL_PASSWORD,
18-
db=MYSQL_DATABASE,
19-
charset='utf8mb4',
20-
cursorclass=pymysql.cursors.DictCursor)
21-
22-
base_sp = BeautifulSoup(requests.get("https://onlinemathcontest.com/contests/all").content)
23-
conn = make_connection()
24-
with conn.cursor() as cursor:
25-
sql = "SELECT url FROM contest_info"
26-
cursor.execute(sql)
27-
contest_urls = [tmp["url"] for tmp in cursor]
28-
29-
for tmp in base_sp.find_all("paper-card"):
30-
if tmp.find("h2") and tmp.find("h2").contents[0] == " Past Contests ":
31-
break
32-
li = tmp.find_all("li")
33-
schedule = li[0].contents[0].strip()
34-
rated = li[1].contents[0].strip()
35-
url = tmp.find("a")["href"]
36-
sp = BeautifulSoup(requests.get(url).content)
37-
title = sp.find("h1").contents[0]
38-
tester = []
39-
for tmp in filter(lambda x: x.contents and "tester" in x.contents[0], sp.find_all("p")):
40-
tester = [a.contents[0].strip() for a in tmp.find_all("a")]
41-
#元々存在するかどうかで場合分け
42-
if url in contest_urls:
43-
print("UPDATE", title)
44-
sql = "UPDATE contest_info SET title = %s, schedule = %s, rated = %s, tester = %s WHERE url = %s"
45-
else:
46-
print("INSERT", title)
47-
sql = "INSERT INTO contest_info (title, schedule, rated, tester, url) VALUES (%s, %s, %s, %s, %s)"
48-
cursor.execute(sql, (title, schedule, rated, ",".join(tester), url))
1+
import requests
2+
from bs4 import BeautifulSoup
3+
import pymysql
4+
from dotenv import load_dotenv
5+
import os
6+
from os.path import join,dirname
7+
8+
dotenv_path=join(dirname(__file__),'.env')
9+
load_dotenv(dotenv_path)
10+
MYSQL_HOST=os.environ.get("MYSQL_HOST")
11+
MYSQL_USER=os.environ.get("MYSQL_USER")
12+
MYSQL_PASSWORD=os.environ.get("MYSQL_PASSWORD")
13+
MYSQL_DATABASE=os.environ.get("MYSQL_DATABASE")
14+
def make_connection():
15+
return pymysql.connect(host=MYSQL_HOST,
16+
user=MYSQL_USER,
17+
password=MYSQL_PASSWORD,
18+
db=MYSQL_DATABASE,
19+
charset='utf8mb4',
20+
cursorclass=pymysql.cursors.DictCursor)
21+
22+
base_sp = BeautifulSoup(requests.get("https://onlinemathcontest.com/contests/all").content)
23+
conn = make_connection()
24+
with conn.cursor() as cursor:
25+
sql = "SELECT url FROM contest_info"
26+
cursor.execute(sql)
27+
contest_urls = [tmp["url"] for tmp in cursor]
28+
29+
for tmp in base_sp.find_all("paper-card"):
30+
if tmp.find("h2") and tmp.find("h2").contents[0] == " Past Contests ":
31+
break
32+
li = tmp.find_all("li")
33+
schedule = li[0].contents[0].strip()
34+
rated = li[1].contents[0].strip()
35+
url = tmp.find("a")["href"]
36+
sp = BeautifulSoup(requests.get(url).content)
37+
title = sp.find("h1").contents[0]
38+
writer = []
39+
for tmp in filter(lambda x: x.contents and "Writer" in x.contents[0], sp.find_all("div")):
40+
writer = [a.contents[0].strip() for a in tmp.find_all("a")]
41+
tester = []
42+
for tmp in filter(lambda x: x.contents and "Tester" in x.contents[0], sp.find_all("p")):
43+
tester = [a.contents[0].strip() for a in tmp.find_all("a")]
44+
#元々存在するかどうかで場合分け
45+
if url in contest_urls:
46+
print("UPDATE", title)
47+
sql = "UPDATE contest_info SET title = %s, schedule = %s, rated = %s, writer = %s, tester = %s WHERE url = %s"
48+
else:
49+
print("INSERT", title)
50+
sql = "INSERT INTO contest_info (title, schedule, rated, writer, tester, url) VALUES (%s, %s, %s, %s, %s, %s)"
51+
cursor.execute(sql, (title, schedule, rated, ",".join(writer), ",".join(tester), url))
4952
conn.commit()

0 commit comments

Comments
 (0)