raannakasturi commited on
Commit
4091161
·
1 Parent(s): e73bcbd

Enhance image upload functionality and update OAuth client secret in post_blog.py and get_refresh_token.py

Browse files
Files changed (2) hide show
  1. get_refresh_token.py +1 -1
  2. post_blog.py +27 -6
get_refresh_token.py CHANGED
@@ -6,7 +6,7 @@ from googleapiclient import discovery
6
 
7
  # Start the OAuth flow to retrieve credentials
8
  def authorize_credentials():
9
- CLIENT_SECRET = 'client_secret_6.json'
10
  SCOPE = 'https://www.googleapis.com/auth/blogger'
11
  STORAGE = Storage('credentials.storage.json')
12
  credentials = STORAGE.get()
 
6
 
7
  # Start the OAuth flow to retrieve credentials
8
  def authorize_credentials():
9
+ CLIENT_SECRET = 'client_secret_4.json'
10
  SCOPE = 'https://www.googleapis.com/auth/blogger'
11
  STORAGE = Storage('credentials.storage.json')
12
  credentials = STORAGE.get()
post_blog.py CHANGED
@@ -20,14 +20,31 @@ def extract_summary(text):
20
  return match.group(1).replace("#", "").replace("\n", "").strip()
21
  return None
22
 
 
 
 
 
 
 
 
23
  def generate_image(title, summary, category):
24
  extracted_summary = extract_summary(summary)
25
  if extracted_summary is None:
26
  extracted_summary = title
27
  url = f"https://image.pollinations.ai/prompt/(({category}))%20{title}%20%3A%20{extracted_summary}?width=1280&height=720&seed=623862700&nologo=true&model=turbo"
28
- return url
 
 
 
 
 
 
29
 
30
  def generate_post_html(title, summary, mindmap, category, citation):
 
 
 
 
31
  image = generate_image(title, summary, category)
32
  html_summary = mistune.html(summary)
33
  post = f"""
@@ -75,14 +92,16 @@ def sanitize_citation(citation):
75
  )
76
  return sanitized_citation
77
 
78
-
79
  def create_post(title, category, summary, mindmap, citation):
80
- post_title = f"{title}"
81
  post_category = f"{category}"
82
  post_body, post_image = generate_post_html(title, summary, mindmap, category, sanitize_citation(citation))
 
 
83
  return post_title, post_category, post_body, post_image
84
 
85
  def post_post(title, category, body, image, i):
 
86
  try:
87
  data = requests.post(
88
  url='https://oauth2.googleapis.com/token',
@@ -113,12 +132,13 @@ def post_post(title, category, body, image, i):
113
  "labels": [category, "recent"]
114
  }
115
  data = requests.post(url, headers=headers, json=post_data).json()
116
- print(data['status'])
117
  if data['status'] == 'LIVE':
118
  return True
119
  else:
 
120
  return False
121
  except Exception as e:
 
122
  print('An error occurred:', str(e))
123
  return False
124
 
@@ -129,9 +149,10 @@ def post_blog(title, category, summary, mindmap, citation, uaccess_key):
129
  post_title, post_category, post_body, post_image = create_post(title, category, summary, mindmap, citation)
130
  i = 1
131
  status = False
132
- while (i <= 6 and not status):
133
  print(f'Posting... Tries: {i}')
134
  status = post_post(post_title, post_category, post_body, post_image, i)
 
135
  i += 1
136
  if status:
137
  print('Post created successfully')
@@ -150,7 +171,7 @@ if __name__ == '__main__':
150
  "2412.16344": {
151
  "id": "2412.16344",
152
  "doi": "https://doi.org/10.48550/arXiv.2412.16344",
153
- "title": "Focal Plane of the Arcus Probe X-Ray Spectrograph",
154
  "category": "Astrophysics",
155
  "citation": "Grant, C. E., Bautz, M. W., Miller, E. D., Foster, R. F., LaMarr, B., Malonis, A., Prigozhin, G., Schneider, B., Leitz, C., &amp; Falcone, A. D. (2024). Focal Plane of the Arcus Probe X-Ray Spectrograph. ArXiv. https://doi.org/10.48550/ARXIV.2412.16344",
156
  "summary": "## Summary\nThe Arcus Probe mission concept provides high-resolution soft X-ray and UV spectroscopy to study the universe. The X-ray Spectrograph (XRS) uses two CCD focal planes to detect and record X-ray photons. Laboratory performance results meet observatory requirements.\n\n## Highlights\n- The Arcus Probe mission concept explores the formation and evolution of clusters, galaxies, and stars.\n- The XRS instrument includes four parallel optical channels and two detector focal plane arrays.\n- The CCDs are designed and manufactured by MIT Lincoln Laboratory (MIT/LL).\n- The XRS focal plane utilizes high heritage MIT/LL CCDs with proven technologies.\n- Laboratory testing confirms CCID-94 performance meets required spectral resolution and readout noise.\n- The Arcus mission includes two co-aligned instruments working simultaneously.\n- The XRS Instrument Control Unit (XICU) controls the activities of the XRS.\n\n## Key Insights\n- The Arcus Probe mission concept provides a significant improvement in sensitivity and resolution over previous missions, enabling breakthrough science in understanding the universe.\n- The XRS instrument's design, including the use of two CCD focal planes and four parallel optical channels, allows for high-resolution spectroscopy and efficient detection of X-ray photons.\n- The CCDs used in the XRS instrument are designed and manufactured by MIT Lincoln Laboratory (MIT/LL), which has a proven track record of producing high-quality CCDs for space missions.\n- The laboratory performance results of the CCID-94 device demonstrate that it meets the required spectral resolution and readout noise for the Arcus mission, indicating that the instrument is capable of achieving its scientific goals.\n- The XRS Instrument Control Unit (XICU) plays a crucial role in controlling the activities of the XRS, including gathering and storing data, and processing event recognition.\n- The Arcus mission's use of two co-aligned instruments working simultaneously allows for a wide range of scientific investigations, including the study of time-domain science and the physics of time-dependent phenomena.\n- The high heritage MIT/LL CCDs used in the XRS focal plane provide a reliable and efficient means of detecting X-ray photons, enabling the instrument to achieve its scientific goals.",
 
20
  return match.group(1).replace("#", "").replace("\n", "").strip()
21
  return None
22
 
23
+ def upload_image(image, api_key):
24
+ url = 'https://api.imgbb.com/1/upload?key=aa38b04047587c609f5c7e22f9d840f0'
25
+ response = requests.post(url, data = {'image': image})
26
+ image_url = response.json()['data']['image']['url']
27
+ status_code = response.json()['status']
28
+ return image_url, status_code
29
+
30
  def generate_image(title, summary, category):
31
  extracted_summary = extract_summary(summary)
32
  if extracted_summary is None:
33
  extracted_summary = title
34
  url = f"https://image.pollinations.ai/prompt/(({category}))%20{title}%20%3A%20{extracted_summary}?width=1280&height=720&seed=623862700&nologo=true&model=turbo"
35
+ data = requests.get(url).content
36
+ encoded_image = base64.b64encode(data)
37
+ image = encoded_image.decode('utf-8')
38
+ image_url, status_code = upload_image(image, os.getenv('IMG_API_KEY'))
39
+ if status_code != 200:
40
+ return f"data:image/png;base64,{image}"
41
+ return image_url
42
 
43
  def generate_post_html(title, summary, mindmap, category, citation):
44
+ title = title.replace("{", r"{{").replace("}", r"}}")
45
+ summary = summary.replace("{", r"{{").replace("}", r"}}")
46
+ mindmap = mindmap.replace("{", r"{{").replace("}", r"}}")
47
+ citation = citation.replace("{", r"{{").replace("}", r"}}")
48
  image = generate_image(title, summary, category)
49
  html_summary = mistune.html(summary)
50
  post = f"""
 
92
  )
93
  return sanitized_citation
94
 
 
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',
 
132
  "labels": [category, "recent"]
133
  }
134
  data = requests.post(url, headers=headers, json=post_data).json()
 
135
  if data['status'] == 'LIVE':
136
  return True
137
  else:
138
+ print(data)
139
  return False
140
  except Exception as e:
141
+ print(data)
142
  print('An error occurred:', str(e))
143
  return False
144
 
 
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 <= 7 and not status):
153
  print(f'Posting... Tries: {i}')
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')
 
171
  "2412.16344": {
172
  "id": "2412.16344",
173
  "doi": "https://doi.org/10.48550/arXiv.2412.16344",
174
+ "title": "On the Interplay of Constraints from $B_s$, $D$, and $K$ Meson Mixing in $Z^\prime$ Models with Implications for $b\to s ν\barν$ Transitions",
175
  "category": "Astrophysics",
176
  "citation": "Grant, C. E., Bautz, M. W., Miller, E. D., Foster, R. F., LaMarr, B., Malonis, A., Prigozhin, G., Schneider, B., Leitz, C., &amp; Falcone, A. D. (2024). Focal Plane of the Arcus Probe X-Ray Spectrograph. ArXiv. https://doi.org/10.48550/ARXIV.2412.16344",
177
  "summary": "## Summary\nThe Arcus Probe mission concept provides high-resolution soft X-ray and UV spectroscopy to study the universe. The X-ray Spectrograph (XRS) uses two CCD focal planes to detect and record X-ray photons. Laboratory performance results meet observatory requirements.\n\n## Highlights\n- The Arcus Probe mission concept explores the formation and evolution of clusters, galaxies, and stars.\n- The XRS instrument includes four parallel optical channels and two detector focal plane arrays.\n- The CCDs are designed and manufactured by MIT Lincoln Laboratory (MIT/LL).\n- The XRS focal plane utilizes high heritage MIT/LL CCDs with proven technologies.\n- Laboratory testing confirms CCID-94 performance meets required spectral resolution and readout noise.\n- The Arcus mission includes two co-aligned instruments working simultaneously.\n- The XRS Instrument Control Unit (XICU) controls the activities of the XRS.\n\n## Key Insights\n- The Arcus Probe mission concept provides a significant improvement in sensitivity and resolution over previous missions, enabling breakthrough science in understanding the universe.\n- The XRS instrument's design, including the use of two CCD focal planes and four parallel optical channels, allows for high-resolution spectroscopy and efficient detection of X-ray photons.\n- The CCDs used in the XRS instrument are designed and manufactured by MIT Lincoln Laboratory (MIT/LL), which has a proven track record of producing high-quality CCDs for space missions.\n- The laboratory performance results of the CCID-94 device demonstrate that it meets the required spectral resolution and readout noise for the Arcus mission, indicating that the instrument is capable of achieving its scientific goals.\n- The XRS Instrument Control Unit (XICU) plays a crucial role in controlling the activities of the XRS, including gathering and storing data, and processing event recognition.\n- The Arcus mission's use of two co-aligned instruments working simultaneously allows for a wide range of scientific investigations, including the study of time-domain science and the physics of time-dependent phenomena.\n- The high heritage MIT/LL CCDs used in the XRS focal plane provide a reliable and efficient means of detecting X-ray photons, enabling the instrument to achieve its scientific goals.",