-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutomatedDownloader.py
More file actions
163 lines (150 loc) · 8.17 KB
/
AutomatedDownloader.py
File metadata and controls
163 lines (150 loc) · 8.17 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import calendar
import datetime
import inspect
import os
import pickle
import win32api
from pyicloud import *
from pyicloud.exceptions import PyiCloudAPIResponseError, PyiCloudFailedLoginException,\
PyiCloudServiceNotActivatedErrror
from requests.exceptions import ConnectionError
import sanityCheck
class Cmd:
def __init__(self):
self.username = self.password = ''
self.list_year, self.list_month, self.list_images, self.count = [], [], [], 0
# To check the Path of the script
self.path = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory
# Variable Initialization for log file
self.message = self.time = self.today = self.phone = ''
self.dev = 0
def get_password_username(self):
# Deciphering your password
a = open(self.path + '/Automation/Credentials/Credentials.txt', 'r+')
raw = a.read().split('*/*/')
count = 1
s1 = ''
l1 = []
for i in raw[0]:
if i == '%':
l1.append(int(s1) - count)
count += 1
s1 = ''
continue
s1 += i
for i in l1:
self.password += chr(i)
# Deciphering your username
l2 = []
raw1 = raw[1].split('@*@')
username = str(raw1[0])
size = int(raw1[1].split('ved')[0])
s = ''
for i in username:
if i == '@':
l2.append(int(s) - size)
size -= 1
s = ''
continue
s += i
for i in l2:
self.username += chr(i)
self.dev = int(raw1[1].split('ved')[1])
def download_photos(self):
try:
# Deciphering Username and password
self.get_password_username()
# Authentication
self.api = PyiCloudService(self.username, self.password)
self.user_name = str(self.api.devices[self.dev]).split(":")[1].strip(' ')
# Sanity Check
self.sanity = sanityCheck.Sanity(self.user_name)
self.sanity.check()
except PyiCloudAPIResponseError:
raise PyiCloudFailedLoginException
except PyiCloudFailedLoginException:
error = 'Invalid email/password combination.\nPlease try again.'
win32api.MessageBox(0, error, 'PyCloud - Message', 0x00000000L + 0x00000010L + 0x00020000L)
except ConnectionError:
error = 'Internet is not working.\nPlease try again.'
win32api.MessageBox(0, error, 'PyCloud - Message', 0x00000000L + 0x00000010L + 0x00020000L)
except:
win32api.MessageBox(0, "Some problem occurred.\nPlease try again.\nTry deleting the folder(with your "
"device's name)", 'PyCloud - Message', 0x00000000L + 0x00000010L + 0x00020000L)
# Reading input data
try:
with open(self.path + '/' + self.user_name + '/AppData/AppData.dat', "rb") as f:
self.list_year = pickle.load(f)
self.list_month = pickle.load(f)
self.list_images = pickle.load(f)
self.count = pickle.load(f)
f.close()
except IOError:
if not os.path.exists(self.user_name + '/AppData'):
folder = self.user_name + '/AppData'
os.makedirs(os.path.join(self.path, folder))
return
try:
# Downloading All Photos from user's iCloud account.
for photo in self.api.photos.all:
if photo.filename not in self.list_images:
download = photo.download()
info = str(photo.created).split()[0].split('-')
year = str(info[0])
month = calendar.month_name[int(info[1])]
if year not in self.list_year:
os.makedirs(os.path.join(self.path + '/' + self.user_name + '/Photos', year))
self.list_year.append(year)
if (month + '_' + year) not in self.list_month:
os.makedirs(os.path.join(self.path + '/' + self.user_name + '/Photos', year, month))
self.list_month.append((month + '_' + year))
self.list_images.append(photo.filename)
with open(self.path + '/' + self.user_name + '/Photos/{}/{}/{}'.format(year, month, photo.filename),
'wb+') as opened_file:
opened_file.write(download.raw.read())
opened_file.close()
self.message += '{} downloaded, Date of creation: {} {}, {}\n'.format(photo.filename, info[2],
calendar.month_abbr[int(info[1])],
info[0])
print '{} downloaded, Date of creation: {} {}, {}'.format(photo.filename, info[2],
calendar.month_abbr[int(info[1])],
info[0])
self.count += 1
with open(self.path + '/' + self.user_name + '/AppData/AppData.dat', "wb") as f:
pickle.dump(self.list_year, f)
pickle.dump(self.list_month, f)
pickle.dump(self.list_images, f)
pickle.dump(self.count, f)
f.close()
except PyiCloudServiceNotActivatedErrror:
error = 'Please log into https://icloud.com/ to\nmanually finish setting up your iCloud service' \
'\n(AUTHENTICATION_FAILED)'
win32api.MessageBox(0, error, 'PyCloud - Message', 0x00000000L + 0x00000010L + 0x00020000L)
self.phone = 'Device: ' + str(self.api.devices[self.dev]).split(':')[0] + '\n' + \
'User:' + str(self.api.devices[self.dev]).split(':')[1]
self.today = list(str(datetime.date.today()).split('-')) # gets today's date
self.time = list(str(datetime.datetime.now().time()).split(':')) # gets current time
self.time = self.time[0] + ':' + self.time[1]
self.message += 'Total Images: {}, Date of Download: {} {}, {} at {}\n'.format(self.count,
self.today[2],
calendar.month_abbr[
int(self.today[1])
],
self.today[0],
self.time)
print 'Total Images: {}, Date of Download: {} {}, {} at {}'.format(self.count,
self.today[2],
calendar.month_abbr[
int(self.today[1])
],
self.today[0],
self.time)
# self.send_mail(self.username, str(self.today[2]), calendar.month_abbr[int(self.today[1])], str(self.today[0]))
# Saving application Log to a text file
log_file = open(self.path + '/' + self.user_name + '/AppData/AppLog.txt', 'a+')
log_file.writelines(self.message + '--------------------------------------------------------------\n')
log_file.close()
win32api.MessageBox(0, self.phone + '\n' + self.message, 'PyCloud - Message',
0x00000001L + 0x00000040L + 0x00020000L)
if __name__ == '__main__':
Cmd().download_photos()