-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
249 lines (217 loc) · 12.9 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import requests
from datetime import datetime as dt
import time
from tqdm import tqdm
from pydrive.auth import GoogleAuth
import io
import json
import configparser
class VK:
def __init__(self, access_token, user_id, version='5.131'):
self.token = access_token
self.id = user_id
self.version = version
self.params = {'access_token': self.token, 'v': self.version}
def get_user_info(self):
url = 'https://api.vk.com/method/users.get'
params = {'user_ids': self.id}
response = requests.get(url, params={**self.params, **params})
return response.json()
def get_albums_info(self):
url = 'https://api.vk.com/method/photos.getAlbums'
params = {'owner_id': self.id, 'need_system': '1'}
response = requests.get(url, params={**self.params, **params})
return response.json()
@staticmethod
def select_album_for_upload(get_albums_info):
print(f'Доступные альбомы фотографий:\n'
f'|')
for albums_ind in range(len(get_albums_info['response']['items'])):
print(f"|- {albums_ind + 1}. [{get_albums_info['response']['items'][albums_ind]['title']}], "
f"({get_albums_info['response']['items'][albums_ind]['size']} фото)")
print()
selected_album = input('Введите номер альбома для сохранения на облачный сервис: ')
if not selected_album.isdigit() \
or int(selected_album) not in range(1, len(get_albums_info['response']['items']) + 1):
print('Ошибка! Введен некорректный номер альбома!')
return False
num_of_photos = input('Введите количество фотографий выбранного альбома для сохранения на облачный сервис: ')
if not num_of_photos.isdigit() \
or int(num_of_photos) not in range(1, get_albums_info['response']['items'][int(selected_album) - 1][
'size'] + 1):
print('Ошибка! Введено некорректное количество фотографий!')
return False
print(f"Выбран альбом [{get_albums_info['response']['items'][int(selected_album) - 1]['title']}], "
f"количество фотографий "
f"[{int(num_of_photos)}/{get_albums_info['response']['items'][int(selected_album) - 1]['size']}]")
return {'album_id': get_albums_info['response']['items'][int(selected_album) - 1]['id'],
'number_of_photos': num_of_photos,
'album_title': get_albums_info['response']['items'][int(selected_album) - 1]['title']}
def get_user_photos_info(self, album_id, number_of_photos):
photos_info = []
file_names = []
url = 'https://api.vk.com/method/photos.get'
params = {'owner_id': self.id, 'album_id': album_id, 'extended': '1',
'photo_sizes': '1', 'count': number_of_photos}
response = requests.get(url, params={**self.params, **params})
for ph_ind in range(len(response.json()['response']['items'])):
file_names.append(str(response.json()['response']['items'][ph_ind]['likes']['count']))
photos_info.append(
{'file_name': str(response.json()['response']['items'][ph_ind]['likes']['count']),
'size': response.json()['response']['items'][ph_ind]['sizes'][-1]['type'],
'vk_photo_url': response.json()['response']['items'][ph_ind]['sizes'][-1]['url'],
'date': dt.fromtimestamp(response.json()['response']['items'][ph_ind]['date']).strftime(
'%d.%m.%Y_%Hч_%Mм_%Sс')})
duplicate_file_names = [file_names[i] for i in range(len(file_names)) if i != file_names.index(file_names[i])]
for ph_ind in range(len(photos_info)):
if photos_info[ph_ind]['file_name'] in duplicate_file_names:
photos_info[ph_ind]['file_name'] += '_' + photos_info[ph_ind]['date'] + '.jpg'
else:
photos_info[ph_ind]['file_name'] += '.jpg'
return photos_info
class YaUploader:
def __init__(self, token: str):
self.token = token
def check_folder(self, folder_name):
folders = []
url = 'https://cloud-api.yandex.net/v1/disk/resources'
params = {'path': '/'}
headers = {'Authorization': f'OAuth {self.token}', 'Accept': 'application/json'}
response = requests.get(url, params=params, headers=headers)
if response.status_code == 200:
for item in range(len(response.json()['_embedded']['items'])):
if response.json()['_embedded']['items'][item]['type'] == 'dir':
folders.append(response.json()['_embedded']['items'][item]['name'])
if folder_name not in folders:
return False
return True
def upload_photo(self, file_path, photo_url):
url = 'https://cloud-api.yandex.net/v1/disk/resources/upload'
params = {'path': file_path, 'url': photo_url, }
headers = {'Authorization': f'OAuth {self.token}'}
response = requests.post(url, params=params, headers=headers)
response_status_operation = requests.get(response.json()['href'], headers=headers)
while response_status_operation.json()['status'] != 'success':
if response_status_operation.json()['status'] == 'failed':
print('Ошибка! Скопировать файл не удалось, переходим к следующему')
return
time.sleep(1)
response_status_operation = requests.get(response.json()['href'], headers=headers)
return response.status_code
def create_folder(self, folder_name):
create_folder_url = 'https://cloud-api.yandex.net/v1/disk/resources'
headers = {'Authorization': f'OAuth {self.token}'}
params = {'path': folder_name}
response = requests.put(create_folder_url, params=params, headers=headers)
if response.status_code == 201:
print('Папка успешно создана')
else:
print(f"Ошибка! {response.json()['message']}")
return response.status_code
class GDrive:
def __init__(self):
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
token = gauth.attr['credentials'].access_token
self.access_token = token
def check_folder(self, folder_name):
url = 'https://www.googleapis.com/drive/v3/files'
headers = {'Authorization': 'Bearer ' + self.access_token}
params = {'q': "name = '" + folder_name + "'"}
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
for item in range(len(response.json()['files'])):
if response.json()['files'][item]['mimeType'] == 'application/vnd.google-apps.folder':
return response.json()['files'][0]['id']
return False
def create_folder(self, folder_name):
url = 'https://www.googleapis.com/upload/drive/v3/files'
headers = {'Authorization': 'Bearer ' + self.access_token}
metadata = {"name": folder_name, "mimeType": "application/vnd.google-apps.folder"}
files = {'data': ('metadata', json.dumps(metadata), 'application/json')}
response = requests.post(url, files=files, headers=headers)
if response.status_code == 200:
print('Папка успешно создана')
return response.json()['id']
print(f"Ошибка! {response.json()['error']['message']}")
return 'error'
def upload_photo(self, folder_id, filename, photo_url):
url = 'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&supportsAllDrives=true'
metadata = {"name": filename, "parents": [folder_id]}
files = {'data': ('metadata', json.dumps(metadata), 'application/json'),
'file': io.BytesIO(requests.get(photo_url).content)}
response = requests.post(url, files=files, headers={"Authorization": "Bearer " + self.access_token})
return response.status_code
def write_json_file(photos_for_upload):
for i in range(len(photos_for_upload)):
del photos_for_upload[i]['date']
del photos_for_upload[i]['vk_photo_url']
with open('photos.json', 'w', encoding='utf-8') as file:
json.dump(photos_for_upload, file, ensure_ascii=False, indent=2)
print('Файл photos.json cоздан')
return
def main():
config = configparser.ConfigParser()
config.read('settings.ini')
access_token_vk = config['VK']['token']
access_token_ya = config['YADisk']['token']
user_id = input('Введите id аккаунта VK: ')
vk = VK(access_token_vk, user_id)
ya_disk = YaUploader(access_token_ya)
user_name = vk.get_user_info()
if len(user_name['response']) == 0:
print('Введен некорректный id')
return
print(f"Имя: {user_name['response'][0]['first_name']} {user_name['response'][0]['last_name']}")
albums = vk.get_albums_info()
selected_album_for_upload = vk.select_album_for_upload(albums)
if not selected_album_for_upload:
return
selected_photos_for_upload = vk.get_user_photos_info(selected_album_for_upload['album_id'],
selected_album_for_upload['number_of_photos'])
folder_for_upload = input('Введите название папки для загрузки или оставьте поле пустым и нажмите [Enter], '
'чтобы именем папки являлось имя альбома: ')
if not folder_for_upload:
folder_for_upload = selected_album_for_upload['album_title']
# Выбираем целевой диск
select_cloud_service = input('Выберите облачный сервис для загрузки: [1] - Яндекс.Диск, [2] - GoogleDrive: ')
if select_cloud_service not in ('1', '2'):
return print('Введено неверное значение. Необходимо ввести 1 или 2')
# Работаем с Я.Диск
elif select_cloud_service == '1':
print('Проверяем есть ли указанная папка в корневой директории яндекс.диска...')
if not ya_disk.check_folder(folder_for_upload):
print('Папка отсутствует. Создаём...')
if ya_disk.create_folder(folder_for_upload) != 201:
return
print('Начинаем процесс копирования...')
else:
print('Папка уже существует в директории. Начинаем процесс копироания...')
for photo in tqdm(range(len(selected_photos_for_upload)), ncols=100, colour='white', desc='Прогресс загрузки',
bar_format='{desc}: {percentage:3.0f}% |{bar}| {n_fmt}/{total_fmt}'):
if ya_disk.upload_photo(folder_for_upload + '/' + selected_photos_for_upload[photo]['file_name'],
selected_photos_for_upload[photo]['vk_photo_url']) != 202:
print('Ошибка выполнения операции')
# Работаем с Google Disk
elif select_cloud_service == '2':
gdisk = GDrive()
print('Проверяем есть ли указанная папка в корневой директории яндекс.диска...')
gdisk_folder_id = gdisk.check_folder(folder_for_upload)
if not gdisk_folder_id:
print('Папка отсутствует. Создаём...')
gdisk_folder_id = gdisk.create_folder(folder_for_upload)
if gdisk_folder_id == 'error':
return
print('Начинаем процесс копирования...')
else:
print('Папка уже существует в директории. Начинаем процесс копироания...')
for photo in tqdm(range(len(selected_photos_for_upload)), ncols=100, colour='white', desc='Прогресс загрузки',
bar_format='{desc}: {percentage:3.0f}% |{bar}| {n_fmt}/{total_fmt}'):
if gdisk.upload_photo(gdisk_folder_id, selected_photos_for_upload[photo]['file_name'],
selected_photos_for_upload[photo]['vk_photo_url']) != 200:
print('Ошибка загрузки файла')
print('Копирование фотографий завершено! Создаем файл json с информацией о файлах')
write_json_file(selected_photos_for_upload)
print('Выполнение программы завершено')
if __name__ == '__main__':
main()