-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
146 lines (126 loc) · 3.81 KB
/
app.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
# -*- coding: utf-8 -*-
from bottle import route,run
from requests_oauthlib import OAuth1Session
import conf
import json
import os
import re
CK = conf.consumer_key
CS = conf.consumer_secret
AT = conf.access_token
AS = conf.access_token_secret
twitter = OAuth1Session(CK,CS,AT,AS)
url = "https://api.twitter.com/1.1/statuses/user_timeline.json"
params = {
"screen_name":"denx_robotaro",
"count" : "1"
}
req = twitter.get(url,params = params)
if req.status_code == 200:
timeline = json.loads(req.text)
for tweet in timeline:
print(tweet["text"])
else:
print("Error:%d"%req.status_code)
"""
def getOC(campus):
name = "denx_robotaro"
if campus == "i":
name = "denx_imadegawa"
params = {
"screen_name":name,
"count" : "10"
}
oc = ""
stat_change_time = ""
req = twitter.get(url,params = params)
if req.status_code == 200:
timeline = json.loads(req.text)
for tweet in timeline:
#statusの更新
tweet_text = tweet["text"]
m = re.search("OPEN|CLOSE",tweet_text)
if m is not None:
stat = {
"status":tweet_text[m.start():m.end()],
"stat_change_time":getTime(tweet_text),
"campus":"imadegawa" if campus == "i" else "kyotanabe"
}
return stat
return "ERROR"
else:
return "ERROR"
"""
def getReq(campus):
name = "denx_robotaro"
if campus == "i":
name = "denx_imadegawa"
params = {
"screen_name":name,
"count" : "10"
}
req = twitter.get(url,params = params)
if req.status_code == 200:
timeline = json.loads(req.text)
c = "imadegawa" if campus == "i" else "kyotanabe"
return getOCstatus(c,timeline)
else:
return req.status_code
def getOCstatus(campus,timeline):
r_timeline = []
for tweet in timeline:
r_timeline.insert(0,tweet)
oc = ""
stat_change_time = ""
if campus == "kyotanabe":
oc_233 = "CLOSE"
oc_234 = "CLOSE"
for tweet in r_timeline:
tweet_text = tweet["text"]
#print(tweet_text)
m = re.search(r"OPEN|CLOSE",tweet_text)
r = re.search(r"\[234\]|\[233\]",tweet_text)
#print(m.group())
#print(r.group())
if r.group() == "[233]":
oc_233 = m.group()
if r.group() == "[234]":
oc_234 = m.group()
stat_change_time = getTime(tweet_text)
#print(oc_233)
#print(oc_234)
if oc_233 == "OPEN" or oc_234 == "OPEN":
oc = "OPEN"
if oc_233 == "CLOSE" and oc_234 == "CLOSE":
oc = "CLOSE"
if campus == "imadegawa":
for tweet in r_timeline:
tweet_text = tweet["text"]
m = re.search(r"OPEN|CLOSE",tweet_text)
if m is not None:
oc = m.group()
else:
oc = "ERROR"
stat_change_time = getTime(tweet_text)
stat = {
"status":oc,
"stat_change_time":stat_change_time,
"campus":campus
}
return stat
def getTime(tweet_text):
m = re.search(r"\d*時\d*分",tweet_text)
if m is not None:
return tweet_text[m.start():m.end()]
else:
return "GET TIME ERROR"
@route('/')
def denden():
return "でんでんでんくす"
@route('/box&c=<campus>',method = 'GET')
def oc_show(campus = 'k'):
#status = getOC(campus)
status = getReq(campus)
return json.dumps(status,ensure_ascii=False)
run(host="0.0.0.0",port=int(os.environ.get("PORT", 5000)))
#run(host="localhost",port=int(os.environ.get("PORT", 5000)))