-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCloudCacheManager.py
236 lines (202 loc) · 6.9 KB
/
CloudCacheManager.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import pickle
import time
from datetime import datetime, timedelta
from NotificationCenter import debug, info, warning, error, critical
from pydrive.drive import GoogleDrive
from pydrive.auth import GoogleAuth
import os
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
cred = credentials.Certificate("h4rvestre4per-firebase-adminsdk-du65m-806725a0ef.json")
firebase_admin.initialize_app(cred)
db = firestore.client()
drive_id = "0XXXXXXXXXXXXXVA" # confirm from url
folder_id = "1_VpYpXiISNr-fFEJVfGa0UTTwFoXVh1K" # confirm from url
# Googleサービスを認証
gauth = GoogleAuth()
# 資格情報ロードするか、存在しない場合は空の資格情報を作成
gauth.LoadCredentialsFile("mycreds.txt")
# Googleサービスの資格情報がない場合
if gauth.credentials is None:
# ユーザーから認証コードを自動的に受信しローカルWebサーバーを設定
gauth.LocalWebserverAuth()
# アクセストークンが存在しないか、期限切れかの場合
elif gauth.access_token_expired:
# Googleサービスを認証をリフレッシュする
gauth.Refresh()
# どちらにも一致しない場合
else:
# Googleサービスを承認する
gauth.Authorize()
# 資格情報をtxt形式でファイルに保存する
gauth.SaveCredentialsFile("mycreds.txt")
# Googleドライブの認証処理
drive = GoogleDrive(gauth)
def set_observing_fire_cache(data):
doc_ref = db.collection('observing')
docs = doc_ref.stream()
for doc in docs:
# print(u'{} => {}'.format(doc.id, doc.to_dict()))
doc_ref.document(doc.id).delete()
doc_ref.add(data)
def get_observing_fire_cache():
data = {}
docs = db.collection('observing').get()
for doc in docs:
dic = doc.to_dict()
for i, j in dic.items():
# firebasetype_to_datetimetype
deadline = datetime.fromtimestamp(dic[i].timestamp())
data[i] = deadline
# print(type(dic['deadline']))
# print(type(deadline))
return data
def set_transactions_fire_cache(data):
doc_ref = db.collection('transactions')
docs = doc_ref.stream()
for doc in docs:
# print(u'{} => {}'.format(doc.id, doc.to_dict()))
doc_ref.document(doc.id).delete()
doc_ref.add(data)
def get_transactions_fire_cache():
try:
data = {}
docs = db.collection('transactions').get()
for doc in docs:
dic = doc.to_dict()
for i, j in dic.items():
# firebasetype_to_datetimetype
data[i] = j
# print(type(dic['deadline']))
# print(type(deadline))
return data
except:
data = {
'user': 0,
'status': False,
'pair': None,
'amount': 0,
'buy_time': None,
'sell_time': None,
'buy_coin': 0,
'sell_coin': 0,
'profit': 0,
'mode': 0,
}
set_transactions_fire_cache(data)
warning("[CacheManager]例外:ファイルがないので初期ファイルを作成します")
return data
# 監視している通貨リスト
def get_monitoring_currency_cache():
try:
query = "'{}' in parents and trashed=false".format(folder_id)
for file_list in drive.ListFile({'q': query}):
for file in file_list:
if file['title'] == 'monitoring_currency_cache.bin':
file.GetContentFile('save/monitoring_currency_cache.bin')
print("download")
else:
print("pass")
# f = drive.CreateFile({'id': file_id})
# f.GetContentFile('save/monitoring_currency_cache.bin')
z = open('save/monitoring_currency_cache.bin', 'rb')
r = pickle.load(z)
return r
except:
data = {}
set_monitoring_currency_cache(data)
return data
def set_monitoring_currency_cache(data):
z = open('save/monitoring_currency_cache.bin', 'wb')
pickle.dump(data, z)
z.close()
file_metadata = {
'id': "1f4PN92WgusiWjhS_UxwUkrgcSO9i8ive",
'title': "monitoring_currency_cache.bin",
'parents': [{
'id': folder_id,
'kind': 'drive#fileLink',
}],
}
f = drive.CreateFile(file_metadata)
# use SetContentFile for attach and upload
f.SetContentFile('save/monitoring_currency_cache.bin')
# always apply param when upload
f.Upload(param={'supportsTeamDrives': True})
print("uploaded")
# 今取引しているコインについての情報
# 取引してないときはNULL?
# userは1or2
def get_position_cache():
try:
query = "'{}' in parents and trashed=false".format(folder_id)
for file_list in drive.ListFile({'q': query}):
for file in file_list:
if file['title'] == 'position_cache.bin':
file.GetContentFile('save/position_cache.bin')
print("download")
else:
print("pass")
path = 'save/position_cache.bin'
with open(path, 'rb') as web:
r = pickle.load(web)
return r
except:
data = {
'user': 0,
'status': False,
'pair': None,
'amount': 0,
'buy_time': None,
'sell_time': None,
'buy_coin': 0,
'sell_coin': 0,
'profit': 0,
'mode': 0,
}
set_position_cache(data)
warning("[CacheManager]例外:ファイルがないので初期ファイルを作成します")
return data
def set_position_cache(data):
path = 'save/position_cache.bin'
with open(path, 'wb') as web:
pickle.dump(data, web)
file_metadata = {
'id': '1QDUlb164YvgfH76Zww8CVYIm4wL__sv9',
'title': 'position_cache.bin',
'parents': [{
'id': folder_id,
'kind': 'drive#fileLink',
}],
}
f = drive.CreateFile(file_metadata)
# use SetContentFile for attach and upload
f.SetContentFile('save/position_cache.bin')
# always apply param when upload
f.Upload(param={'supportsTeamDrives': True})
class CacheManagerClass:
def __init__(self):
pass
if __name__ == "__main__":
dict = {
'status': False,
'dt_now': None,
'price': 0,
'usecoin': None,
'amount': 0,
'wasOverbuy': False,
'wasOversold': False,
'crossoverbuy': False,
'crossoversold': False,
'buy_coin': 0,
'sell_coin': 0,
'mode': 0,
}
data = {'XEMUSDT': datetime.now() + timedelta(hours=1),
'BATUSDT': datetime.now() + timedelta(hours=1),
'BTCUSDT': datetime.now() + timedelta(hours=1)
}
print(get_transactions_fire_cache())
# set_position_cache(dict)
# set_monitoring_currency_cache(data)