forked from nagaoka252510/dsc_readup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctrl_db.py
215 lines (154 loc) · 5.54 KB
/
ctrl_db.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from sqlalchemy import create_engine, orm
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Boolean, Float, Date, ForeignKey
import datetime
import psycopg2
import json
# dbのユーザとか読み込み
with open('token.json') as f:
df = json.load(f)
Base = declarative_base()
class News(Base):
__tablename__ = 'news'
id = Column(Integer, primary_key=True)
category = Column(String)
text = Column(String)
ardnotify = orm.relationship("ArdNotify")
def __repr__(self):
return "<id={}, cat={}, text={}>".format(self.id, self.category, self.text)
class ArdNotify(Base):
__tablename__ = 'ardnotify'
id = Column(Integer, primary_key=True)
server_id = Column(String, ForeignKey('guild.id', onupdate='CASCADE', ondelete='CASCADE'))
news_id = Column(Integer, ForeignKey('news.id', onupdate='CASCADE', ondelete='CASCADE'))
class Guild(Base):
__tablename__ = 'guild'
id = Column(String, primary_key=True)
name = Column(String)
prefix = Column(String)
is_nameread = Column(Boolean, default=False)
ardnotify = orm.relationship("ArdNotify")
dictionaly = orm.relationship("Dictionaly")
class User(Base):
__tablename__ = 'user'
id = Column(String, primary_key=True)
name = Column(String)
speaker = Column(String)
speed = Column(Float, default=1)
r_range = Column(Float, default=1.1)
pitch = Column(Float, default=1.2)
class Dictionaly(Base):
__tablename__ = 'dictionaly'
id = Column(Integer, primary_key=True, autoincrement=True)
word = Column(String)
read = Column(String)
server_id = Column(String, ForeignKey('guild.id', onupdate='CASCADE', ondelete='CASCADE'))
class CountRequest(Base):
__tablename__ = 'countrequest'
id = Column(Integer, primary_key=True, autoincrement=True)
date = Column(Date)
hour = Column(Integer)
count = Column(Integer, default = 0)
url = 'postgresql+psycopg2://{}@localhost/{}'.format(df['db_user'], df['db_name'])
engine = create_engine(url)
def main():
Base.metadata.create_all(engine)
if __name__ == "__main__":
main()
Session = orm.sessionmaker(bind=engine)
session = Session()
def add_news(cat, mess):
news = News(category=cat, text=mess)
session.add(news)
session.commit()
def get_news():
news = session.query(News)
return news
def add_notify(news_id, guild_id):
notified = ArdNotify(server_id=guild_id ,news_id=news_id)
session.add(notified)
session.commit()
def get_notify(guild_id):
notifications = session.query(ArdNotify).filter_by(server_id=guild_id)
return notifications
def add_guild(guild_id, name, prefix):
guild = Guild(id=guild_id, name=name, prefix=prefix)
session.add(guild)
session.commit()
def set_prefix(guild_id, prefix):
guild = session.query(Guild).filter_by(id=guild_id).one()
guild.prefix = prefix
session.commit()
def get_guild(guild_id):
guilds = session.query(Guild).filter_by(id=guild_id).one_or_none()
return guilds
def add_user(user_id, name, speaker):
user = User(id=user_id, name=name, speaker=speaker)
session.add(user)
session.commit()
def set_user(user_id, speaker):
user = session.query(User).filter_by(id=user_id).one()
user.speaker = speaker
session.commit()
def get_user(user_id):
users = session.query(User).filter_by(id=user_id).one_or_none()
return users
def add_dict(word, read, server_id):
dictionary = session.query(Dictionaly).filter_by(word=word, server_id=server_id).one_or_none()
if isinstance(dictionary, type(None)):
dictionary = Dictionaly(word=word, read=read, server_id=server_id)
session.add(dictionary)
session.commit()
else:
set_dict(read, dictionary)
def set_dict(read, dictionary):
dictionary.read = read
session.commit()
def get_dict(server_id):
dictionary = session.query(Dictionaly).filter_by(server_id=server_id)
return dictionary
def del_dict(id, str_id):
found_dict = session.query(Dictionaly).filter_by(id=id, server_id=str_id).one_or_none()
if isinstance(found_dict, type(None)):
return None
else:
session.delete(found_dict)
session.commit()
return True
def set_nameread(is_read, guild_id):
found_guild = session.query(Guild).filter_by(id=guild_id).one_or_none()
if isinstance(found_guild, type(None)):
return None
else:
found_guild.is_nameread = is_read
session.commit()
def set_readspeed(prm, id):
found_user = session.query(User).filter_by(id=id).one_or_none()
if isinstance(found_user, type(None)):
return None
else:
found_user.speed = prm
session.commit()
def set_readrange(prm, id):
found_user = session.query(User).filter_by(id=id).one_or_none()
if isinstance(found_user, type(None)):
return None
else:
found_user.r_range = prm
session.commit()
def set_readpitch(prm, id):
found_user = session.query(User).filter_by(id=id).one_or_none()
if isinstance(found_user, type(None)):
return None
else:
found_user.pitch = prm
session.commit()
def set_reqcount(date, time):
found_rec = session.query(CountRequest).filter_by(date = date, hour = time).one_or_none()
if isinstance(found_rec, type(None)):
newct = CountRequest(date = date, hour = time)
session.add(newct)
session.commit()
found_rec = session.query(CountRequest).filter_by(date = date, hour = time).one()
found_rec.count += 1
session.commit()