-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdb.py
333 lines (289 loc) · 11.5 KB
/
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import aiohttp
from motorengine import Document
from motorengine import StringField, IntField, BooleanField, ListField, FloatField
from motorengine import EmailField, EmbeddedDocumentField
import motorengine.errors
import logging
import time
from cid import make_cid
_OAUTH_USER_URL = 'https://api.github.com/user'
class Wallet(Document):
likeid = StringField()
filecoin = StringField()
class Author(Document):
name = StringField(required=True)
url = StringField()
avatar = StringField()
description = StringField()
wallet = EmbeddedDocumentField(embedded_document_type=Wallet)
# id = StringField() # TODO: user's ipns profile, json&html
# github_id = IntField()
class Share(Document):
__collection__ = "share2" # optional
id = StringField(required=True, unique=True) # ipfs:// ipns:// not cid
idx = IntField(required=True, unique=True) # just for fun
publisher_id = IntField(required=True) # we use github_id
title = StringField(required=True)
filetype = StringField(required=True)
license = StringField()
summary = StringField()
content_html = StringField()
url = StringField() # URLField()
tags = ListField(StringField(required=True, max_length=255)) # required=True
authors = ListField(EmbeddedDocumentField(embedded_document_type=Author))
# created_at = StringField()
# update_at = StringField()
# for filecoin
data_cid = StringField()
miner_ids = ListField(StringField(required=True, max_length=255))
class GithubAuth(Document):
# basic
token = StringField(required=True)
id = IntField(required=True)
login = StringField(required=True)
node_id = StringField(required=True)
name = StringField(required=True)
avatar_url = StringField(required=True)
created_at = StringField(required=True)
updated_at = StringField(required=True)
email = EmailField(required=True) # private
# optional
followers = IntField(required=True)
following = IntField(required=True)
public_gists = IntField(required=True)
public_repos = IntField(required=True)
location = StringField(required=True)
company = StringField(required=True)
blog = StringField(required=True)
site_admin = BooleanField()
hireable = BooleanField()
bio = StringField()
twitter_username = StringField()
class Meta(Document):
__collection__ = "meta4" # optional
path = StringField(required=True)
eth = StringField(required=True)
name = StringField(required=True)
image = StringField(required=True)
tags = ListField(StringField(required=True, max_length=255)) # required=True
authors = StringField(required=True)
idx = IntField(required=True, unique=True) # just for fun
created_at = FloatField()
class Metaversion(Document):
__collection__ = "metaversion4" # optional
mid = StringField(required=True) # mongo_id
cidnew = StringField(required=True)
cidold = StringField(required=True) # verbose
async def fetch_user(token):
headers = {}
headers['Authorization'] = 'token {}'.format(token)
async with aiohttp.ClientSession() as session:
async with session.get(_OAUTH_USER_URL, headers=headers) as resp:
return await resp.json()
async def get_github_info(token):
auth = await GithubAuth.objects.get(token=token)
if auth:
return auth.to_son()
else:
user = await fetch_user(token)
user['token'] = token
l_keys = 'html events followers following gists organizations received_events repos starred subscriptions'
for k in l_keys.split():
user.pop('{}_url'.format(k))
user.pop('url')
_auth = GithubAuth(**user)
_auth = await _auth.save()
return user
async def add_share(doc, token):
cid = doc['id']
if not cid.startswith('ipfs://') and not cid.startswith('ipns://'):
return 'Error: Content ID should startswith ipfs:// or ipns://'
shares = await Share.objects.filter(id=cid).limit(1).find_all()
if shares: # should update by same publisher_id
try:
ashare = shares[0]
_id = ashare._id
share = Share(_id=_id, **doc)
share.idx = ashare.idx
user = await get_github_info(token)
if ashare.publisher_id != user['id']:
return 'publisher should be same'
share.publisher_id = user['id']
if share.authors and len(share.authors) == 1:
author = {}
name = share.authors[0].get('name')
author['name'] = name if name else user['name']
url = share.authors[0].get('url')
author['url'] = url if url else user['blog']
wallet = share.authors[0].get('wallet')
if wallet:
author['wallet'] = Wallet(**wallet)
share.authors = [author]
share.authors = [Author(**author) for author in share.authors]
share = await share.save(upsert=True)
share.authors = [author.to_son() for author in share.authors]
return share
except motorengine.errors.InvalidDocumentError as e:
logging.error(e)
return e
try:
share = Share(**doc)
user = await get_github_info(token)
share.publisher_id = user['id']
if share.authors and len(share.authors) == 1:
author = {}
name = share.authors[0].get('name')
author['name'] = name if name else user['name']
url = share.authors[0].get('url')
author['url'] = url if url else user['blog']
wallet = share.authors[0].get('wallet')
if wallet:
author['wallet'] = Wallet(**wallet)
share.authors = [author]
share.authors = [Author(**author) for author in share.authors]
share.idx = await Share.objects.count() + 1 # TODO 2
share = await share.save()
share.authors = [author.to_son() for author in share.authors]
return share
except motorengine.errors.InvalidDocumentError as e:
logging.error(e)
return e
async def add_meta(path, eth, name, image, tags, authors):
try:
doc = {}
doc['eth'] = eth
doc['name'] = name
if 'https://ipfs.infura.io' in image: # v1
id0 = image.replace('https://ipfs.infura.io/ipfs/','')
cid = make_cid(id0)
id1 = cid.to_v1().encode('base32').decode()
image = 'https://{}.ipfs.infura-ipfs.io/'.format(id1)
if path.startswith('Qm'):
cid = make_cid(path)
path = cid.to_v1().encode('base32').decode()
doc['path'] = path
doc['image'] = image
doc['tags'] = tags
doc['authors'] = authors
doc['idx'] = await Meta.objects.count() + 1
doc['created_at'] = time.time()
meta = Meta(**doc)
meta = await meta.save()
return meta
except motorengine.errors.InvalidDocumentError as e:
print(e)
logging.error(e)
return e
async def edit_meta(previous_path, path, eth, name, image, tags, authors):
try:
if 'https://ipfs.infura.io' in image:
id0 = image.replace('https://ipfs.infura.io/ipfs/','')
cid = make_cid(id0)
id1 = cid.to_v1().encode('base32').decode()
image = 'https://{}.ipfs.infura-ipfs.io/'.format(id1)
if path.startswith('Qm'):
cid = make_cid(path)
path = cid.to_v1().encode('base32').decode()
if previous_path.startswith('Qm'):
cid = make_cid(previous_path)
previous_path = cid.to_v1().encode('base32').decode()
doc = {}
doc['eth'] = eth
doc['name'] = name
doc['path'] = path
doc['image'] = image
doc['tags'] = tags
doc['authors'] = authors
doc['updated_at'] = time.time()
metas = await Meta.objects.filter(path=previous_path).limit(1).find_all()
if metas: # should update by same publisher_id
try:
ameta = metas[0]
_id = ameta._id
doc['idx'] = ameta.idx
meta = Meta(_id=_id, **doc)
meta = await meta.save(upsert=True)
docv = {}
docv['mid'] = str(_id)
docv['cidold'] = previous_path
docv['cidnew'] = path
metav = Metaversion(**docv)
metav = await metav.save()
return meta
except motorengine.errors.InvalidDocumentError as e:
logging.error(e)
return e
else:
return 'no doc'
except motorengine.errors.InvalidDocumentError as e:
print(e)
logging.error(e)
return e
async def get_meta(eth=None, tag=None):
l_shares = []
if eth:
if eth == '*':
shares = await Meta.objects.order_by('idx').find_all()
else:
shares = await Meta.objects.filter(eth=eth.lower()).find_all()
elif tag:
# shares = await Meta.objects.filter(eth=eth.lower()).find_all()
shares = await Meta.objects.filter(tags=tag.lower()).find_all()
if shares:
for share in shares:
l_shares.append(share._values)
return l_shares[::-1]
async def search_shares(question):
# TODO: add elasticsearch or MeiliSearch
# init
l_shares = []
l_cids = set()
l_searched = set()
# search by title # x times # lower upper capitalize
l_searched.add(question)
shares = await Share.objects.filter(title=question).order_by('idx').limit(5).find_all()
for share in shares:
if share.id not in l_cids:
share.authors = [author.to_son() for author in share.authors]
item = share._values
l_shares.append(item)
l_cids.add(share.id)
if question.upper() not in l_searched:
l_searched.add(question.upper())
shares = await Share.objects.filter(title=question.upper()).order_by('idx').limit(5).find_all()
for share in shares:
if share.id not in l_cids:
share.authors = [author.to_son() for author in share.authors]
item = share._values
l_shares.append(item)
l_cids.add(share.id)
if question.capitalize() not in l_searched:
l_searched.add(question.capitalize())
shares = await Share.objects.filter(title=question.capitalize()).order_by('idx').limit(5).find_all()
for share in shares:
if share.id not in l_cids:
share.authors = [author.to_son() for author in share.authors]
item = share._values
l_shares.append(item)
l_cids.add(share.id)
# tags
shares = await Share.objects.filter(tags=question.lower()).order_by('idx').limit(42).find_all()
for share in shares:
share.authors = [author.to_son() for author in share.authors]
item = share._values
if share.id not in l_cids:
l_shares.append(item)
l_cids.add(share.id)
# search by tags, it's slow now
if not l_shares:
shares = await Share.objects.filter(tags__contains=question).order_by('idx').limit(42).find_all()
for share in shares:
share.authors = [author.to_son() for author in share.authors]
item = share._values
if share.id not in l_cids:
l_shares.append(item)
l_cids.add(share.id)
return l_shares
async def get_shares():
shares = await Share.objects.limit(10).find_all()
return shares