-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
195 lines (155 loc) · 6.6 KB
/
__main__.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import os
import sys
import re
import socket
import time
import threading
from src.pdfconverter import PDFConverter, shutil
from src.configreader import ConfigReader
from src.syncer import Syncer
from pathlib import Path
from icloudpy import ICloudPyService
files_queue = {}
# Create a server socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('localhost', 8000))
def queue_file(data):
# data has a format of PDF_FILE_PATH:::CHANGED;;;PDF_FILE_PATH:::CHANGED (CHANGED is a boolean)
# split data into a list of files
files = data.split(';;;')
# iterate through files and add them to the queue
for file in files:
# split file into file path and changed
splited = file.split(':::')
if (len(splited) != 2):
continue
file_data = splited[0]
changed = splited[1]
files_queue[file_data] = changed
def listen_for_files():
while True:
# Listen for incoming connections
server_socket.listen()
print("Listening for connection...")
client_socket, addr = server_socket.accept()
# Receive data from client
data = client_socket.recv(8192).decode('utf-8')
queue_file(data)
print("Received data: " + data)
client_socket.send(b'Received data')
client_socket.close()
config = ConfigReader().load_config()
api = ICloudPyService(config['AUTH']['id'])
if api.requires_2fa:
print("Two-factor authentication required.")
code = input("Enter the code you received of one of your approved devices: ")
result = api.validate_2fa_code(code)
print("Code validation result: %s" % result)
if not result:
print("Failed to verify security code")
sys.exit(1)
if not api.is_trusted_session:
print("Session is not trusted. Requesting trust...")
result = api.trust_session()
print("Session trust result %s" % result)
if not result:
print("Failed to request trust. You will likely be prompted for the code again in the coming weeks")
elif api.requires_2sa:
import click
print("Two-step authentication required. Your trusted devices are:")
devices = api.trusted_devices
for i, device in enumerate(devices):
print(" %s: %s" % (i, device.get('deviceName',
"SMS to %s" % device.get('phoneNumber'))))
device = click.prompt('Which device would you like to use?', default=0)
device = devices[device]
if not api.send_verification_code(device):
print ("Failed to send verification code")
sys.exit(1)
code = click.prompt('Please enter validation code')
if not api.validate_verification_code(device, code):
print ("Failed to verify verification code")
sys.exit(1)
api.drive.params["clientId"] = api.client_id
# Start the thread to handle incoming connections
threading.Thread(target=listen_for_files).start()
# Main loop
while (True):
if len(files_queue) == 0:
# periodically check for files that are uploaded to icloud
print("Checking for files...")
try:
files = api.drive[config['AUTH']['dump_folder']].dir()
print(files)
for file in files:
# check if file has an nbn extension
if file.endswith('.nbn'):
print(file)
# move the file to the correct location
api.drive[config['AUTH']['dump_folder']][file].move("FOLDER::" + config['AUTH']['destination_name'] + "::Documents")
# remove .nbn from the file
file = file[:-4]
# add .zip to the file
file = file + ".zip"
try:
api.drive[config['AUTH']['dump_folder']][file].delete()
except Exception as e:
print(e)
api.drive[config['AUTH']['dump_folder']].mkdir(file + "buf")
break;
except Exception as e:
print(e)
break;
time.sleep(50)
elif len(files_queue) > 0:
# get first file in queue
pdf_file_path, changed = files_queue.popitem()
pdf_file_name = pdf_file_path.split('/')[-1]
# loop through config and check if pdf_file_name matches regex_match
for section in config:
# skip auth section
if section == 'AUTH':
continue
if re.match(config[section]['regex_match'], pdf_file_name):
# pdf_file_name matches regex_match
if not Path(config[section]['reference_path']).expanduser().is_dir():
print("reference path for " + section + " is invalid!")
break
pdf_file_path = os.path.join(config[section]['working_dir'], pdf_file_path)
print("Now handling: " + pdf_file_name)
pdfconverter = PDFConverter(pdf_file_path, config[section]['reference_path'], pdf_file_name)
pdfconverter.convert()
# check if pdfconverter converted by checking if the file exists
# remove .pdf from pdf_file_name
pdf_file_name = pdf_file_name[:-4]
converted_file_name = pdf_file_name
if not Path(converted_file_name).is_dir():
print("PDFConverter failed to convert!")
break
# create zip file of converted file
shutil.make_archive(converted_file_name, 'zip', converted_file_name)
# delete original
shutil.rmtree(converted_file_name)
# sync converted file
syncer = Syncer(api.drive,
config['AUTH']['dump_folder'],
config['AUTH']['destination_name'],
converted_file_name + ".zip",
)
try:
syncer.sync(changed)
except Exception as e:
# remove zip file
os.remove(converted_file_name + ".zip")
# add file back to queue
files_queue[pdf_file_path] = changed
print(e)
print("job failed; continuing in 5")
time.sleep(5)
break
# remove zip file
os.remove(converted_file_name + ".zip")
print("finished job: " + converted_file_name)
time.sleep(1)
break
print("skipping: " + pdf_file_name)