-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraperTools.py
120 lines (96 loc) · 3.04 KB
/
scraperTools.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
import requests
from bs4 import BeautifulSoup
import re
import json
import pickle
import sys
import os
import numpy as np
def getProjectLinksOffpage(page):
URL = 'https://oshwlab.com/explore?projectSort=updatedTime&page=' + str(page)
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
project_containers = soup.find_all("a", class_="project-link")
projectLinks=[]
for project in project_containers:
projectLinks.append(project.get('href'))
return projectLinks
def getFileIds(projectLink):
projectPage = requests.get("https://oshwlab.com" + projectLink)
projectPageSoup = BeautifulSoup(projectPage.content, 'html.parser')
button = projectPageSoup.find("a", text="Open all in editor")
if button is not None:
fileLink = button.get('href')
print(fileLink)
ids = re.split(r'\||=',fileLink)
ids = ids[1:]
if '' in ids:
ids.remove('')
else:
ids = []
return ids
def getFileData(id):
projectSourceAPI_URL = "https://easyeda.com/api/documents/" + id + "?version=6.4.7&uuid=" + id
print(projectSourceAPI_URL)
projectSource = requests.get(projectSourceAPI_URL)
return projectSource.json()
def getPCB_data(text):
if '"docType":3' not in text:
return None
start = text.index('shape":[') + len('shape":[')
openCount = 0
end = None
for i in range(start, len(text)):
if text[i] =="]":
openCount -= 1
if openCount == -1:
end = i
break
elif text[i] == "[":
openCount += 1
if end == None:
return None #file has mismatching [ ] pairs
shapeData = text[start:end]
jsonData = json.loads(text[:start-1] +'"tmp"'+ text[end+1:])
if "result" in jsonData:
if "dataStr" in jsonData["result"]:
jsonData["result"]["dataStr"] = shapeData
return jsonData
return None
def getDataFromFile(path):
with open(path, 'rb') as fileHandle:
return pickle.load(fileHandle)
def saveDataToFile(path, data):
with open(path, "wb") as fileHandle:
pickle.dump(data, fileHandle)
def seqSaveDataToFile(path, data):
with open(path, 'ab') as output: # Note: `ab` appends the data
pickle.dump(data, output)
def seqGetDataFromFile(path):
objs = []
with open(path, 'rb') as f:
while 1:
try:
objs.append(pickle.load(f))
except EOFError:
break
return objs
def getNthFromFile(path, i):
with open(path, 'rb') as f:
count = 0
while(count < i):
try:
pickle.load(f)
except EOFError:
print("i exceeds list size")
raise
count += 1
return pickle.load(f)
def deleteOldFile(path):
try:
os.remove(path)
except FileNotFoundError:
print("File not found")
def saveDataToFileInd(path, data):
with open(path, "wb") as f:
np.save(f, data)