raannakasturi commited on
Commit
22c0846
·
1 Parent(s): 1ca1182

Enhance image handling with timeout and improve post HTML structure

Browse files
Files changed (3) hide show
  1. image.py +2 -2
  2. main.py +1 -1
  3. post_blog.py +30 -31
image.py CHANGED
@@ -45,7 +45,7 @@ def generate_image(title, summary):
45
  return None
46
  image_url = image.params.get("url")
47
  if image_url:
48
- img_data = requests.get(image_url).content
49
  base64_encoded = base64.b64encode(img_data).decode("utf-8")
50
  return f"data:image/png;base64,{base64_encoded}"
51
  return None
@@ -68,7 +68,7 @@ def upload_image(data_uri, api_key):
68
  try:
69
  base64_image = fix_base64_padding(data_uri.split(",")[1])
70
  url = f"https://api.imgbb.com/1/upload?key={api_key}"
71
- response = requests.post(url, data={"image": base64_image}).json()
72
  if response.get("status") == 200:
73
  image_url = response["data"]["display_url"]
74
  else:
 
45
  return None
46
  image_url = image.params.get("url")
47
  if image_url:
48
+ img_data = requests.get(image_url, timeout=60).content
49
  base64_encoded = base64.b64encode(img_data).decode("utf-8")
50
  return f"data:image/png;base64,{base64_encoded}"
51
  return None
 
68
  try:
69
  base64_image = fix_base64_padding(data_uri.split(",")[1])
70
  url = f"https://api.imgbb.com/1/upload?key={api_key}"
71
+ response = requests.post(url, data={"image": base64_image}, timeout=60).json()
72
  if response.get("status") == 200:
73
  image_url = response["data"]["display_url"]
74
  else:
main.py CHANGED
@@ -40,7 +40,7 @@ def paper_data(paper_data, wait_time=5):
40
  data = json.dumps(data, indent=4, ensure_ascii=False)
41
  return data
42
 
43
- def post_blogpost(uaccess_key, wait_time=3):
44
  if uaccess_key != access_key:
45
  return False
46
  data = fetch_paper_data_with_category(uaccess_key)
 
40
  data = json.dumps(data, indent=4, ensure_ascii=False)
41
  return data
42
 
43
+ def post_blogpost(uaccess_key, wait_time=5):
44
  if uaccess_key != access_key:
45
  return False
46
  data = fetch_paper_data_with_category(uaccess_key)
post_blog.py CHANGED
@@ -22,37 +22,36 @@ def generate_post_html(title, summary, mindmap, citation):
22
  image = fetch_image(title, summary, imgbb_api_key)
23
  html_summary = mistune.html(summary)
24
  post = f"""
25
- <div>
26
- <script src="https://cdn.jsdelivr.net/npm/markmap-autoloader@latest"></script>
27
- <style>
28
- .markmap {{
29
- position: relative;
30
- }}
31
- .markmap > svg {{
32
- width: 100%;
33
- border: 2px solid #000;
34
- height: 80dvh;
35
- }}
36
- </style>
37
- <img style='display:block; width:100%;height:100%;' id='paper_image' src='{image}' />
38
- <br>
39
- <b>{{getToc}} $title={{Table of Contents}}</b>
40
- <br>
41
- <p id="paper_summary" data="{summary.replace("&amp;", "&")}">{html_summary.replace("&amp;", "&")}</p>
42
- <br>
43
- <br>
44
- <h2>Mindmap</h2>
45
- <div class="markmap" id="paper_mindmap" data="# {title} \n {mindmap.replace("&amp;", "&")}">
46
- <script type="text/template">
47
- {mindmap.replace("&amp;", "&")}
48
- </script>
49
- </div>
50
- <br>
51
- <h2>Citation</h2>
52
- <p id="paper_citation" data="{citation.replace("&amp;", "&")}">
53
- {mistune.html(citation.replace("&amp;", "&"))}
54
- </p>
55
- </div>
56
  """
57
  return post, image
58
 
 
22
  image = fetch_image(title, summary, imgbb_api_key)
23
  html_summary = mistune.html(summary)
24
  post = f"""
25
+ <div id="paper_post">
26
+ <script src="https://cdn.jsdelivr.net/npm/markmap-autoloader@latest"></script>
27
+ <style>
28
+ .markmap {{
29
+ position: relative;
30
+ }}
31
+ .markmap > svg {{
32
+ width: 100%;
33
+ border: 2px solid #000;
34
+ height: 80dvh;
35
+ }}
36
+ </style>
37
+ <img style='display:block; width:100%;height:100%;' id='paper_image' src='{image}' />
38
+ </br>
39
+ <b>{{getToc}} $title={{Table of Contents}}</b>
40
+ <br>
41
+ <p id="paper_summary">{html_summary.replace("&amp;", "&")}</p>
42
+ </br>
43
+ <h2>Mindmap</h2>
44
+ <div class="markmap" id="paper_mindmap">
45
+ <script type="text/template">
46
+ {mindmap.replace("&amp;", "&").replace(":", "=>")}
47
+ </script>
48
+ </div>
49
+ </br>
50
+ <h2>Citation</h2>
51
+ <p id="paper_citation">
52
+ {mistune.html(citation.replace("&amp;", "&"))}
53
+ </p>
54
+ </div>
 
55
  """
56
  return post, image
57