-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotpopmenu.py
53 lines (47 loc) · 2.62 KB
/
spotpopmenu.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
from lib.dialog import ListMDDialog
import requests
import certifi
import shutil
import os.path
import filecmp
def ImageCheck(imageSource, imageName, fileName, SpotName, iD):
try:
# Check if image is already downloaded/created and if it is different to the error.png
if os.path.isfile(f"cache/{fileName}{SpotName}{iD}.png") and filecmp.cmp(f"cache/{fileName}{SpotName}{iD}.png", "lib/error.png") == False:
print('[' + '\x1b[1;32;40m' + 'INFO' + '\x1b[0m' +
f' ] [{imageName} ] ' + "Check succedeed - Image found - Cached Image will be used")
# If image cannot be used from cache try to download the image
else:
print('[' + '\x1b[1;33;40m' + 'INFO' + '\x1b[0m' +
f' ] [{imageName} ] ' + "Cache check failed - Image will be downloaded")
# Download image if the image link in database is not empty
if imageSource != "":
print('[' + '\x1b[1;32;40m' + 'INFO' + '\x1b[0m' +
f' ] [{imageName} ] ' + f"Found Image source - using {imageSource}")
response = requests.get(
str(imageSource), verify=certifi.where())
file = open(f"cache/{fileName}{SpotName}{iD}.png", "wb")
file.write(response.content)
file.close()
# Use the missing.jpg if there is no image link in database
else:
print('[' + '\x1b[1;31;40m' + 'INFO' + '\x1b[0m' +
f' ] [{imageName} ] ' + "Missing Image - using missing.jpg")
shutil.copy("lib/missing.jpg",
f"cache/{fileName}{SpotName}{iD}.png")
except:
print('[' + '\x1b[1;31;40m' + 'INFO' + '\x1b[0m' +
f' ] [{imageName} ] ' + "Download failed - Device Offline, using error.png")
shutil.copy("lib/error.png", f"cache/{fileName}{SpotName}{iD}.png")
class LocationPopupMenu(ListMDDialog):
def __init__(self, spot_data):
super().__init__()
headers = ['ID', 'SpotName', 'x', 'y', 'MapsLink', 'Website', 'Street', 'City',
'Country', 'PLZ', 'Image1', 'Image2', 'Image3', 'Information', 'Category', 'updateTime']
for i in range(len(headers)):
attribute_name = headers[i]
attribute_value = str(spot_data[i])
setattr(self, attribute_name, attribute_value)
ImageCheck(self.Image1, "Image 1", "image1", self.SpotName, self.ID)
ImageCheck(self.Image2, "Image 2", "image2", self.SpotName, self.ID)
ImageCheck(self.Image3, "Image 3", "image3", self.SpotName, self.ID)