Repository navigation
Expand file tree
/
Copy pathnpmSearch.py
More file actions
67 lines (57 loc) · 1.94 KB
/
Copy pathnpmSearch.py
File metadata and controls
67 lines (57 loc) · 1.94 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
59
60
61
62
63
64
65
66
67
import aiohttp
from aiofile import async_open
import json
import asyncio
import sys
npmSearchApi = "https://npmmirror.com/package/{keyword}"
async def checkPackage(keyword):
url = npmSearchApi.format(keyword=keyword)
async with aiohttp.ClientSession() as session:
async with session.get(url=url) as req:
status_code = req.status
if status_code != 200:
print(keyword,"不存在")
async def getPackageByUrl(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as req:
if req.status ==200:
return json.loads(await req.text())
return False
async def getPackageByFile(filepath):
async with async_open(filepath,"r") as fhand:
text = await fhand.read()
return json.loads(text)
async def asyncList(slist):
__tmpArray = []
listSize = len(slist)
listIndex = 0
for k,v in slist.items():
__tmpArray.append(k)
while True:
if listIndex > listSize - 1:
break
yield __tmpArray[listIndex]
listIndex = listIndex + 1
def printHelp():
print(sys.argv[0] + " --url https://github.com/github/package.json")
print(sys.argv[0] + " --file ./package.json")
async def main():
#async with aiohttp.ClientSession() as session:
if "--url" in sys.argv:
packageJson = await getPackageByUrl(sys.argv[2])
elif "--file" in sys.argv:
packageJson = await getPackageByFile(sys.argv[2])
else:
printHelp()
exit()
packageList = {}
packageList.update(packageJson.get("dependencies",{}))
packageList.update(packageJson.get("devDependencies",{}))
if len(packageList) == 0:
exit()
print(packageJson.get("repository",{}).get("url",""))
async for p in asyncList(packageJson):
await checkPackage(p)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())