raannakasturi commited on
Commit
1a5b0c0
·
1 Parent(s): f64b2ad

Refactor post_blog and post_post functions to improve readability and maintainability

Browse files
Files changed (1) hide show
  1. post_blog.py +15 -7
post_blog.py CHANGED
@@ -95,21 +95,21 @@ def sanitize_citation(citation):
95
  def create_post(title, category, summary, mindmap, citation):
96
  post_title = title
97
  post_category = f"{category}"
98
- post_body, post_image = generate_post_html(title, summary, mindmap, category, sanitize_citation(citation))
99
  with open('post.html', 'w', encoding='utf-8') as f:
100
  f.write(post_body)
101
  return post_title, post_category, post_body, post_image
102
 
103
- def post_post(title, category, body, image, i):
104
  data = None
105
  try:
106
  data = requests.post(
107
  url='https://oauth2.googleapis.com/token',
108
  data={
109
  'grant_type': 'refresh_token',
110
- 'client_secret': os.getenv(f'CLIENT_SECRET_{i}'),
111
- 'refresh_token': os.getenv(f'REFRESH_TOKEN_{i}'),
112
- 'client_id': os.getenv(f'CLIENT_ID_{i}'),
113
  },
114
  ).json()
115
  url = f"https://blogger.googleapis.com/v3/blogs/{blog_id}/posts"
@@ -147,6 +147,7 @@ def post_blog(title, category, summary, mindmap, citation, uaccess_key):
147
  return False
148
  try:
149
  post_title, post_category, post_body, post_image = create_post(title, category, summary, mindmap, citation)
 
150
  i = 1
151
  status = False
152
  while (i <= 2 and not status):
@@ -154,6 +155,11 @@ def post_blog(title, category, summary, mindmap, citation, uaccess_key):
154
  status = post_post(post_title, post_category, post_body, post_image, i)
155
  # time.sleep(30)
156
  i += 1
 
 
 
 
 
157
  if status:
158
  print('Post created successfully')
159
  return True
@@ -164,7 +170,7 @@ def post_blog(title, category, summary, mindmap, citation, uaccess_key):
164
  print('An error occurred:', str(e))
165
  return False
166
 
167
- if __name__ == '__main__':
168
  data = {
169
  "status": "success",
170
  "Astrophysics": {
@@ -191,4 +197,6 @@ if __name__ == '__main__':
191
  mindmap = paperdata['mindmap']
192
  citation = paperdata['citation']
193
  access_key = access_key
194
- post_blog(title, category, summary, mindmap, citation, access_key)
 
 
 
95
  def create_post(title, category, summary, mindmap, citation):
96
  post_title = title
97
  post_category = f"{category}"
98
+ post_body, post_image = generate_post_html(title, summary, mindmap, category, citation)
99
  with open('post.html', 'w', encoding='utf-8') as f:
100
  f.write(post_body)
101
  return post_title, post_category, post_body, post_image
102
 
103
+ def post_post(title, category, body, image):
104
  data = None
105
  try:
106
  data = requests.post(
107
  url='https://oauth2.googleapis.com/token',
108
  data={
109
  'grant_type': 'refresh_token',
110
+ 'client_secret': client_secret,
111
+ 'refresh_token': refresh_token,
112
+ 'client_id': client_id,
113
  },
114
  ).json()
115
  url = f"https://blogger.googleapis.com/v3/blogs/{blog_id}/posts"
 
147
  return False
148
  try:
149
  post_title, post_category, post_body, post_image = create_post(title, category, summary, mindmap, citation)
150
+ <<<<<<< HEAD
151
  i = 1
152
  status = False
153
  while (i <= 2 and not status):
 
155
  status = post_post(post_title, post_category, post_body, post_image, i)
156
  # time.sleep(30)
157
  i += 1
158
+ =======
159
+ status = post_post(post_title, post_category, post_body, post_image)
160
+ print(f"Waiting for {3*60} seconds...")
161
+ time.sleep(3*60)
162
+ >>>>>>> 56dd9ba597776577f8fa4c53e0f0f15ec5d72960
163
  if status:
164
  print('Post created successfully')
165
  return True
 
170
  print('An error occurred:', str(e))
171
  return False
172
 
173
+ def test(access_key):
174
  data = {
175
  "status": "success",
176
  "Astrophysics": {
 
197
  mindmap = paperdata['mindmap']
198
  citation = paperdata['citation']
199
  access_key = access_key
200
+ status = post_blog(title, category, summary, mindmap, citation, access_key)
201
+ print(status)
202
+ return status