-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageScript.py
56 lines (49 loc) · 2.14 KB
/
imageScript.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
import json
import requests
MODELS_LIST_PATH=r"C:\Users\abhin\Desktop\modelsList.txt"
MODELS_LIST_PATH=r"C:\Users\abhin\Desktop\brandsList.txt"
CAR_MODEL_IMAGES_PATH='C:\\Users\\abhin\\WebstormProjects\\shaanu\\carModelsImages\\'
CAR_BRAND_IMAGES_PATH='C:\\Users\\abhin\\WebstormProjects\\shaanu\\carBrandsImages\\'
JSON_PATH=r"C:\Users\abhin\WebstormProjects\shaanu\src\database\carsData.json"
def downloadToFolder(txtPath,destinationFolder,imageLinkFoundArgument="https",imageLinkNotFoundArgument="PIC NOT AVAILABLE",extenstion="jpeg",defaultPic="https://raw.githubusercontent.com/abhinaypandey02/shaanu/master/carModelsImages/0.jpeg"):
models=[]
key=0
with open(txtPath,'r') as f:
for line in f.readlines():
if imageLinkFoundArgument in line:
models.append(line.strip())
if imageLinkNotFoundArgument in line:
models.append(defaultPic)
for url in models:
print("Downloading image "+str(key)+'.'+extenstion)
r=requests.get(url, allow_redirects=True)
print("Writing image ")
with open(destinationFolder+str(key)+'.'+extenstion,'wb') as f:
f.write(r.content)
key+=1
def updateJSON(jsonPath,modifyJSON):
jsonData={}
with open(jsonPath,'r') as f:
jsonData=json.loads(f.read())
modifyJSON(jsonData)
with open(jsonPath,'w') as f:
f.write(json.dumps(jsonData))
def setModelURLs(jsonData):
key=0
for brand in jsonData:
for model in jsonData[brand]["models"]:
try:
jsonData[brand]["models"][model]["imageURL"]="https://raw.githubusercontent.com/abhinaypandey02/shaanu/master/carModelsImages/"+str(key)+".jpeg"
key+=1
print(key)
except:
break
def setBrandURLs(jsonData):
key=0
for brand in jsonData:
jsonData[brand]["imageURL"]="https://raw.githubusercontent.com/abhinaypandey02/shaanu/master/carBrandsImages/"+str(key)+".jpeg"
key+=1
print(key)
downloadToFolder(MODELS_LIST_PATH,CAR_MODEL_IMAGES_PATH)
# updateJSON(JSON_PATH,setModelURLs)
updateJSON(JSON_PATH,setBrandURLs)