-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9.requests.lesson.py
More file actions
68 lines (50 loc) · 1.7 KB
/
9.requests.lesson.py
File metadata and controls
68 lines (50 loc) · 1.7 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
import os
from pprint import pprint
import requests
GIFS_DIR = "gifs"
response = requests.get(
"https://www.reddit.com/r/gifs.json",
headers={"User-Agent": "netology"}
)
if response.status_code != 200:
raise Exception("всё очень плохо")
# response.raise_for_status() - можно заменить условие выше
posts = response.json()["data"]["children"]
for post in posts:
title: str = post["data"]["title"]
url = post["data"]["url"]
if "imgur.com/" not in url:
continue
url = url.replace(".gifv", ".gif")
gif_resp = requests.get(url)
gif_resp.raise_for_status()
title = "".join(x for x in title if x.isalnum() or x.isspace())
with open(os.path.join(GIFS_DIR, title + ".gif"), "wb") as f:
f.write(gif_resp.content)
print(title)
# --------------
# получить список файлов на яндекс диске
HEADERS = {"Autorization": "OAuth dsfdfsewfwwebfwbdw"}
resp = requests.get(
"https://cloud-api.yandex.net/v1/disk/resources",
params={"path": "/"},
headers=HEADERS,
)
resp.raise_for_status()
data = resp.json()
for file in data['_embedded']["items"]:
print(file["name"])
# Получить ссылку на загрузку файлов на яндекс диск
resp1 = requests.get(
"https://cloud-api.yandex.net/v1/disk/resources/upload",
params={"path": "/awesome.gif", "overwrite": "true"},
headers=HEADERS,
)
resp1.raise_for_status()
d = resp1.json()
# pprint(d) # получили ссылку href и метод PUT
href = d["href"]
# Отправляем файл
with open("gifs/ddfd.gif", "rb") as f:
resp2 = requests.put(href, files={"file": f})
resp2.raise_for_status()