-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequests.py
More file actions
59 lines (49 loc) · 1.8 KB
/
requests.py
File metadata and controls
59 lines (49 loc) · 1.8 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
46
47
48
49
50
51
52
53
54
55
56
57
58
import asyncio
import aiohttp
def initSemaphore():
global semaphore
semaphore = asyncio.Semaphore(64)
async def asyncRequest(endpoint):
async with semaphore:
try:
async with aiohttp.ClientSession() as session:
async with session.get("http://127.0.0.1:4002"+endpoint, timeout=10) as response:
data = await response.json()
if type(data) == dict and data.get("success") == False:
return "failure"
else:
return data
except asyncio.TimeoutError:
return "timeout"
async def fetchConnectedPeers():
endpoint = "/ob/peers"
connectedPeers = await asyncRequest(endpoint)
if type(connectedPeers) == list and len(connectedPeers) > 0 and type(connectedPeers[0]) == str:
return connectedPeers
else:
return []
async def getCrawlPeers(peerID):
async def fetchCrawlPeers(endpoint):
peers = await asyncRequest(endpoint)
if type(peers) == list and len(peers) > 0 and type(peers[0]) == str:
return peers
else:
return []
endpoints = ["/ob/closestpeers/{}".format(peerID),
"/ob/following/{}".format(peerID),
"/ob/followers/{}".format(peerID)]
crawlPeers = await asyncio.gather(*[fetchCrawlPeers(endpoint) for endpoint in endpoints])
crawlPeers = [y for x in crawlPeers for y in x]
crawlPeers = list(set(crawlPeers))
return crawlPeers
async def peerOnline(peerID):
endpoint = "/ob/status/{}".format(peerID)
status = await asyncRequest(endpoint)
if type(status) == dict and status.get("status") == "online":
return [peerID, True]
else:
return [peerID, False]
async def main():
pass
if __name__ == "__main__":
asyncio.run(main())