dialect+driver://username:password@host:port/database
db = 'postgresql://postgres:admin@localhost:5432/postgres'
engine = sqlalchemy.create_engine(db)
connection = engine.connect()
class YandexDisk:#
def __init__(self, token_ya):
self.token = token_ya
def get_headers(self):
return {
'Content-Type': 'application/json',
'Authorization': 'OAuth {}'.format(self.token)
}
def _get_upload_link(self, disk_file_path):
upload_url = 'https://cloud-api.yandex.net/v1/disk/resources/upload'
headers = self.get_headers()
params = {'path': disk_file_path, 'overwrite': 'true'}
response = requests.get(upload_url, headers=headers, params=params)
pprint(response.json())
return response.json()
def upload_file_to_disk(self, disk_file_path, file_name_jpg):
href_json = self._get_upload_link(disk_file_path=disk_file_path)
href = href_json['href']
response = requests.put(href, data=open(file_name_jpg, 'rb'))
response.raise_for_status()
if response.status_code == 201:
print('Success')
if __name__ == '__main__':
vk = VkUser(token_vk, '5.131')
vk.save_photo('5.131')
class VkUser:
def __init__ (self, token_vk, version):
self.params = {
'access_token': token_vk,
'v': version
}
class VkUser:
url = 'https://api.vk.com/method/'
def __init__ (self, token_vk, version):
self.params = {
'access_token': token_vk,
'v': version
}
def save_photo(self, version):
url_photo = 'https://api.vk.com/method/photos.get'
params = {
'owner_id': id,
'album_id': 'profile',
'rev': False,
'extended': True,
'photo_sizes': False,
'access_token': token_vk,
'count': 5,
'v': '5.131'
}
res = requests.get(url_photo, params=params)
# pprint(res)
res_str = res.json()
# pprint(res_str)
res_str_response = res_str['response']['items']
# pprint(res_str_response)
count = 0
for item in res_str_response:
link = item['sizes'][-1]['url']
# print(link)
file_name = str(item['likes']['count'])
# print(file_name)
file_name_jpg = file_name + '.jpg'
# print(file_name_jpg)
dict = {'file_name': file_name_jpg}
size = item['sizes'][-1]['type']
size_x = {'size': size}
list = [dict | size_x]
# print(list)
list_json = json.dumps(list)
# print(list_json)
foto_download = requests.get(link)
with open(file_name_jpg, 'wb') as file:
file.write(foto_download.content)
count += 1
print(count, 'photo', file_name_jpg, 'downloaded from VK')
ya.upload_file_to_disk('List/photo.jpg', 'photo.jpg')
Все таки подключился с помощью PyCharm.