Spaces:
Running
Running
Commit
·
2713c62
1
Parent(s):
d785842
Add error handling to post_post function and adjust sleep duration
Browse files- post_blog.py +38 -34
post_blog.py
CHANGED
@@ -76,38 +76,42 @@ def create_post(title, category, summary, mindmap, citation):
|
|
76 |
|
77 |
def post_post(title, category, body, image):
|
78 |
data = None
|
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 |
return False
|
112 |
|
113 |
def post_blog(title, category, summary, mindmap, citation, uaccess_key):
|
@@ -116,8 +120,8 @@ def post_blog(title, category, summary, mindmap, citation, uaccess_key):
|
|
116 |
else:
|
117 |
post_title, post_category, post_body, post_image = create_post(title, category, summary, mindmap, citation)
|
118 |
status = post_post(post_title, post_category, post_body, post_image)
|
119 |
-
print(f"Waiting for {
|
120 |
-
time.sleep(
|
121 |
if status:
|
122 |
print('Post created successfully')
|
123 |
return True
|
|
|
76 |
|
77 |
def post_post(title, category, body, image):
|
78 |
data = None
|
79 |
+
try:
|
80 |
+
data = requests.post(
|
81 |
+
url='https://oauth2.googleapis.com/token',
|
82 |
+
data={
|
83 |
+
'grant_type': 'refresh_token',
|
84 |
+
'client_secret': client_secret,
|
85 |
+
'refresh_token': refresh_token,
|
86 |
+
'client_id': client_id,
|
87 |
+
},
|
88 |
+
).json()
|
89 |
+
url = f"https://blogger.googleapis.com/v3/blogs/{blog_id}/posts"
|
90 |
+
headers = {
|
91 |
+
'Authorization': f"Bearer {data['access_token']}",
|
92 |
+
"content-type": "application/json"
|
93 |
+
}
|
94 |
+
post_data = {
|
95 |
+
"kind": "blogger#post",
|
96 |
+
"blog": {
|
97 |
+
"id": blog_id
|
98 |
+
},
|
99 |
+
"images": [{
|
100 |
+
"url": image
|
101 |
+
}],
|
102 |
+
"title": title,
|
103 |
+
"content": body,
|
104 |
+
"labels": [category, "recent"]
|
105 |
+
}
|
106 |
+
data = requests.post(url, headers=headers, json=post_data).json()
|
107 |
+
if data['status'] == 'LIVE':
|
108 |
+
print(f"The post '{title}' is {data['status']}")
|
109 |
+
return True
|
110 |
+
else:
|
111 |
+
print(f"Error posting {title}: {data}")
|
112 |
+
return False
|
113 |
+
except Exception as e:
|
114 |
+
print(f"Error posting {title}: {e}")
|
115 |
return False
|
116 |
|
117 |
def post_blog(title, category, summary, mindmap, citation, uaccess_key):
|
|
|
120 |
else:
|
121 |
post_title, post_category, post_body, post_image = create_post(title, category, summary, mindmap, citation)
|
122 |
status = post_post(post_title, post_category, post_body, post_image)
|
123 |
+
print(f"Waiting for {2*60} seconds...")
|
124 |
+
time.sleep(2*60)
|
125 |
if status:
|
126 |
print('Post created successfully')
|
127 |
return True
|