-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
45 lines (36 loc) · 1.41 KB
/
db.py
File metadata and controls
45 lines (36 loc) · 1.41 KB
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
import asyncpg
import asyncio
async def dbInit():
global connectionPool
connectionPool = await createConnectionPool()
await createTables()
async def createConnectionPool():
return await asyncpg.create_pool()
async def createTables():
sql = """CREATE TABLE IF NOT EXISTS crawlPeers(peerID TEXT PRIMARY KEY);"""
async with connectionPool.acquire() as connection:
await connection.execute(sql)
async def inDatabase(peerID):
sql = "SELECT peerID FROM crawlPeers WHERE peerID = $1;"
async with connectionPool.acquire() as connection:
result = await connection.fetch(sql, peerID)
return [peerID, True] if len(result) > 0 else [peerID, False]
async def insertPeers(peers):
async def insertPeer(peerID):
try:
sql = "INSERT INTO crawlPeers (peerID) VALUES ($1);"
async with connectionPool.acquire() as connection:
await connection.execute(sql, peerID)
print("peer inserted:", peerID)
except asyncpg.exceptions.UniqueViolationError:
print("peer already inserted:", peerID)
await asyncio.gather(*[insertPeer(peerID) for peerID in peers])
async def getPeers():
sql = ("SELECT peerID FROM crawlPeers;")
async with connectionPool.acquire() as connection:
peers = await connection.fetch(sql)
return peers
async def main():
await dbInit()
if __name__ == "__main__":
asyncio.run(main())