forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotlight.py
65 lines (55 loc) · 2.48 KB
/
spotlight.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
""" Script To Copy Spotlight(Lockscreen) Images from Windows """
import os
import shutil
import errno
import hashlib
from PIL import Image
def md5(fname):
""" Function to return the MD5 Digest of a file """
hash_md5 = hashlib.md5()
with open(fname, "rb") as file_var:
for chunk in iter(lambda: file_var.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
def make_folder(folder_name):
"""Function to make the required folers"""
try:
os.makedirs(folder_name)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(folder_name):
pass
else:
print "Error! Could not create a folder"
raise
def get_spotlight_wallpapers(target_folder):
"""Fetches wallpapers from source folder inside AppData to the
newly created folders in C:\\Users\\['user.name']\\Pictures"""
#PATHS REQUIRED TO FETCH AND STORE WALLPAPERS
#Creating necessary folders
source_folder = os.environ['HOME']+"\\AppData\\Local\\Packages\\"
source_folder += "Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy"
source_folder += "\\LocalState\\Assets"
spotlight_path_mobile = target_folder+"\\Mobile"
spotlight_path_desktop = target_folder+"\\Desktop"
make_folder(spotlight_path_mobile)
make_folder(spotlight_path_desktop)
#Fetching files from the source dir
for filename in os.listdir(source_folder):
filename = source_folder+"\\"+filename
#if size of file is less than 100 KB, ignore the file
if os.stat(filename).st_size > 100000:
#Check resolution and classify based upon the resolution of the images
#name the file equal to the MD5 of the file, so that no duplicate files are to be copied
img_file = Image.open(filename)
if img_file.size[0] >= 1080:
if img_file.size[0] > img_file.size[1]:
temp_path = spotlight_path_desktop+"\\"+md5(filename)
else:
temp_path = spotlight_path_mobile+"\\"+md5(filename)
#If file doesn't exist, copy the file to the new folders
if not os.path.exists(temp_path+".png"):
shutil.copy(filename, temp_path+".png")
if __name__ == '__main__':
PATH = raw_input("Enter directory path:")
get_spotlight_wallpapers(PATH)
print "Lockscreen images have been copied to \""+PATH+"\""