-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
181 lines (151 loc) · 4.83 KB
/
main.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import time
import requests
import json
import datetime
import sqlite3
import tweepy
# 検索用のパラメータ
ENDPOINT='https://connpass.com/api/v1/event/'
keyword_or='福岡,fukuoka'
start=1
count=50
order=3 #新着順
# 絞り込み用のパラメータ
address_matcher=['福岡','北九州','fukuoka']
# DB情報
DB_NAME='connpass-ITEventFukuoka.sqlite3'
TABLE_NAME='events'
# Twitter API情報
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
def search_events():
url = f"{ENDPOINT}?start={start}&count={count}&keyword_or={keyword_or}&order={order}"
headers = {
'User-Agent':'Mozilla/5.0'
}
response = requests.get(url=url, headers=headers)
status_code=response.status_code
if status_code != 200:
print(f"status_code={status_code} url={url}")
print(response.text)
print("exit")
exit()
results = json.loads(response.text)
return results['events']
def filter_events(events):
results = []
for event in events:
# 終了時刻が過去のイベントは除外
ended_at = event['ended_at'][:-6] # 2023-06-14T20:30:00+09:00 の 末尾の 6文字を 削除し datetime に変換できるようにする
if datetime.datetime.strptime(ended_at, "%Y-%m-%dT%H:%M:%S") < datetime.datetime.now():
print(f"continue: old event : {ended_at}")
continue
# 住所が空のイベントは除外
address = event['address']
if address is None:
print("continue: address is None")
continue
# 住所がマッチしないイベントは除外
matched = False
for matcher in address_matcher:
if matcher in address:
matched = True
break
if not matched:
print("continue: address is not matched")
continue
# DBにあるイベントは除外
if existsInTable(event['event_id']):
print("continue: exists in table")
continue
print(f"target: {event['event_id']} {event['title']}")
results.append(event)
return results
def existsInTable(event_id):
exists = False
conn = sqlite3.connect(DB_NAME)
c = conn.cursor()
c.execute(f'SELECT * FROM {TABLE_NAME} where event_id = {event_id}')
rows = c.fetchall()
for row in rows:
#print(row)
exists = True
break
conn.close()
return exists
def tweet_events(events):
for event in events:
post_text = create_post_txt(event)
#exit()
result = post_tweet(post_text)
print('---------')
if result:
save_event(event)
time.sleep(1)
def create_post_txt(event):
# タイトル(必須)
post_text = event['title']
# 期間
start = ""
end = ""
try:
fmt = "%Y-%m-%dT%H:%M:%S"
started_at = datetime.datetime.strptime(event['started_at'][:-6], fmt)
ended_at = datetime.datetime.strptime(event['ended_at'][:-6], fmt)
# 月と日は0埋めしない
fmt = "%-m/%-d %H:%M"
start = started_at.strftime(fmt)
end = ended_at.strftime(fmt)
# 同日開催ならば end の方には日付は表記しない
if started_at.strftime("%d") == ended_at.strftime("%d"):
end = ended_at.strftime("%H:%M")
except Exception as e:
print(f"Exception : {e}")
if start and end:
post_text += f"\n{start}~{end}"
# 開催会場
place = event['place']
if place:
post_text += '\n' + place
# 開催場所
address = event['address']
if address:
post_text += '\n' + address
# ハッシュタグ
hash_tag = event['hash_tag']
if hash_tag:
post_text += '\n#' + hash_tag
# connpass.com 上のURL
post_text += '\n' + event['event_url']
print(post_text)
return post_text
def post_tweet(post_text):
client = tweepy.Client(
consumer_key=consumer_key, consumer_secret=consumer_secret,
access_token=access_token, access_token_secret=access_token_secret
)
try:
response = client.create_tweet(
#text="[メンテナンス中] これは Twitter API v2 のテストです。"
#text="[メンテナンス終了] Twitter API v2 に移行いたしました。"
text=post_text
)
except Exception as e:
print(f"Exception : {e}")
return False
print(f"tweet : https://twitter.com/user/status/{response.data['id']}")
return True
def save_event(event):
conn = sqlite3.connect(DB_NAME)
c = conn.cursor()
c.execute(f"INSERT INTO {TABLE_NAME}( event_id ) VALUES ( {event['event_id']} )")
conn.commit()
conn.close()
def main():
events = search_events()
events = filter_events(events)
tweet_events(events)
if __name__ == "__main__":
main()