Spaces:
Running
Running
Commit
·
14fbe6d
1
Parent(s):
b9ed792
Refactor post_blog and related functions to include DOI as a parameter for improved post generation
Browse files- main.py +1 -1
- post_blog.py +58 -28
main.py
CHANGED
@@ -40,7 +40,7 @@ def paper_data(paper_data, wait_time=5):
|
|
40 |
except:
|
41 |
encoded_bytes = repr(citation)
|
42 |
citation = html.unescape(encoded_bytes)
|
43 |
-
post_blog(title, category, summary, mindmap, citation, access_key, wait_time)
|
44 |
except Exception as e:
|
45 |
print(f"Error posting blog '{title}': {e}")
|
46 |
continue
|
|
|
40 |
except:
|
41 |
encoded_bytes = repr(citation)
|
42 |
citation = html.unescape(encoded_bytes)
|
43 |
+
post_blog(doi, title, category, summary, mindmap, citation, access_key, wait_time)
|
44 |
except Exception as e:
|
45 |
print(f"Error posting blog '{title}': {e}")
|
46 |
continue
|
post_blog.py
CHANGED
@@ -25,57 +25,87 @@ def generate_mindmap_svg(title, mindmap):
|
|
25 |
)
|
26 |
return result.strip()
|
27 |
|
28 |
-
def generate_post_html(title, summary, mindmap, citation):
|
|
|
|
|
29 |
title = title.replace("{", r'{').replace("}", r'}')
|
|
|
30 |
summary = summary.replace("{", r'{').replace("}", r'}')
|
31 |
mindmap = mindmap.replace("{", r'{').replace("}", r'}')
|
32 |
citation = citation.replace("{", r'{').replace("}", r'}')
|
|
|
33 |
image = fetch_image(title, summary, imgbb_api_key)
|
34 |
-
svg_url = generate_mindmap_svg(title, mindmap)
|
35 |
html_summary = mistune.html(summary)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
post = f"""
|
37 |
-
|
38 |
-
<
|
39 |
-
|
40 |
-
<
|
41 |
-
.markmap {{
|
42 |
-
position: relative;
|
43 |
-
}}
|
44 |
-
.markmap > svg {{
|
45 |
-
width: 100%;
|
46 |
-
border: 2px solid #000;
|
47 |
-
height: 80dvh;
|
48 |
-
}}
|
49 |
-
</style>
|
50 |
-
<img style="display:block; width:100%; height:100%;" id="paper_image" src="{image.strip()}" alt="{title.strip()}">
|
51 |
<br>
|
52 |
-
<
|
53 |
<br>
|
54 |
<div id="paper_summary">
|
55 |
{html_summary.replace("&", "&").strip()}
|
56 |
</div>
|
57 |
<br>
|
58 |
<h2>Mindmap</h2>
|
59 |
-
<
|
60 |
-
|
61 |
-
<script type="text/template">
|
62 |
-
{mindmap.replace("&", "&").replace(":", "=>").strip()}
|
63 |
-
</script>
|
64 |
</div>
|
65 |
<br>
|
66 |
<h2>Citation</h2>
|
67 |
<div id="paper_citation">
|
68 |
-
{
|
69 |
</div>
|
70 |
</div>
|
71 |
"""
|
72 |
return post, image
|
73 |
|
74 |
-
def create_post(title, category, summary, mindmap, citation):
|
75 |
post_title = title
|
76 |
post_category = f"{category}"
|
77 |
try:
|
78 |
-
post_body, post_image = generate_post_html(title, summary, mindmap, citation)
|
|
|
|
|
|
|
79 |
except Exception as e:
|
80 |
print(f"Error generating post: {e}")
|
81 |
return None, None, None, None
|
@@ -126,12 +156,12 @@ def post_post(title, category, body, image):
|
|
126 |
print(f"Error posting {title}: {e}")
|
127 |
return False
|
128 |
|
129 |
-
def post_blog(title, category, summary, mindmap, citation, uaccess_key, wait_time=5):
|
130 |
if uaccess_key != access_key:
|
131 |
return False
|
132 |
else:
|
133 |
status = True
|
134 |
-
post_title, post_category, post_body, post_image = create_post(title, category, summary, mindmap, citation)
|
135 |
if not all([post_title, post_category, post_body, post_image]):
|
136 |
print(f'Failed to create post {post_title}')
|
137 |
return False
|
@@ -185,7 +215,7 @@ def test(uaccess_key):
|
|
185 |
except:
|
186 |
encoded_bytes = repr(citation)
|
187 |
citation = html.unescape(encoded_bytes)
|
188 |
-
status = post_blog(title, category, summary, mindmap, citation, uaccess_key, 0)
|
189 |
print(status)
|
190 |
return status
|
191 |
|
|
|
25 |
)
|
26 |
return result.strip()
|
27 |
|
28 |
+
def generate_post_html(doi, title, category, summary, mindmap, citation):
|
29 |
+
doi = doi.split("https://")[-1]
|
30 |
+
print(f"Generating post for {doi}")
|
31 |
title = title.replace("{", r'{').replace("}", r'}')
|
32 |
+
title = re.sub(r"\\(?![a-zA-Z])", r"", title)
|
33 |
summary = summary.replace("{", r'{').replace("}", r'}')
|
34 |
mindmap = mindmap.replace("{", r'{').replace("}", r'}')
|
35 |
citation = citation.replace("{", r'{').replace("}", r'}')
|
36 |
+
citation = mistune.html(citation.replace("&", "&").strip())
|
37 |
image = fetch_image(title, summary, imgbb_api_key)
|
38 |
+
# svg_url = generate_mindmap_svg(title, mindmap)
|
39 |
html_summary = mistune.html(summary)
|
40 |
+
# post = f"""
|
41 |
+
# <div id="paper_post">
|
42 |
+
# <script src="https://cdn.jsdelivr.net/npm/markmap-autoloader"></script>
|
43 |
+
# <script src="https://cdn.jsdelivr.net/npm/markmap-toolbar"></script>
|
44 |
+
# <style>
|
45 |
+
# .markmap {{
|
46 |
+
# position: relative;
|
47 |
+
# }}
|
48 |
+
# .markmap > svg {{
|
49 |
+
# width: 100%;
|
50 |
+
# border: 2px solid #000;
|
51 |
+
# height: 80dvh;
|
52 |
+
# }}
|
53 |
+
# </style>
|
54 |
+
# <img style="display:block; width:100%; height:100%;" id="paper_image" src="{image.strip()}" alt="{title.strip()}">
|
55 |
+
# <br>
|
56 |
+
# <b>{{getToc}} $title={{Table of Contents}}</b>
|
57 |
+
# <br>
|
58 |
+
# <div id="paper_summary">
|
59 |
+
# {html_summary.replace("&", "&").strip()}
|
60 |
+
# </div>
|
61 |
+
# <br>
|
62 |
+
# <h2>Mindmap</h2>
|
63 |
+
# <p><small><em>If MindMap doesn't load, go to the <a href="/">Homepage</a> and visit blog again or <a href="/#">Switch to Android App (Under Development)</a>.</em></small></p>
|
64 |
+
# <div class="markmap" id="paper_mindmap" data-markmap-url="{svg_url}">
|
65 |
+
# <script type="text/template">
|
66 |
+
# {mindmap.replace("&", "&").replace(":", "=>").strip()}
|
67 |
+
# </script>
|
68 |
+
# </div>
|
69 |
+
# <br>
|
70 |
+
# <h2>Citation</h2>
|
71 |
+
# <div id="paper_citation">
|
72 |
+
# {mistune.html(citation.replace("&", "&").strip())}
|
73 |
+
# </div>
|
74 |
+
# </div>
|
75 |
+
# """
|
76 |
post = f"""
|
77 |
+
<div id="paper_post" data={doi}>
|
78 |
+
<h1 id="paper_title">{title}</h1>
|
79 |
+
<small><em>DOI: <a href="https://{doi}" target="_blank">{doi}</a></em></small>
|
80 |
+
<em>Category: {category}</em>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
<br>
|
82 |
+
<img style="display:block; width:100%; height:100%;" id="paper_image" src="{image.strip()}" alt="{title.strip()}">
|
83 |
<br>
|
84 |
<div id="paper_summary">
|
85 |
{html_summary.replace("&", "&").strip()}
|
86 |
</div>
|
87 |
<br>
|
88 |
<h2>Mindmap</h2>
|
89 |
+
<div id="paper_mindmap">
|
90 |
+
{repr(mindmap.replace("&", "&").strip())}
|
|
|
|
|
|
|
91 |
</div>
|
92 |
<br>
|
93 |
<h2>Citation</h2>
|
94 |
<div id="paper_citation">
|
95 |
+
{citation.replace("&", "&").strip()}
|
96 |
</div>
|
97 |
</div>
|
98 |
"""
|
99 |
return post, image
|
100 |
|
101 |
+
def create_post(doi, title, category, summary, mindmap, citation):
|
102 |
post_title = title
|
103 |
post_category = f"{category}"
|
104 |
try:
|
105 |
+
post_body, post_image = generate_post_html(doi, title, category, summary, mindmap, citation)
|
106 |
+
# with open('post.html', 'w') as f:
|
107 |
+
# f.write(post_body)
|
108 |
+
# exit(code=0)
|
109 |
except Exception as e:
|
110 |
print(f"Error generating post: {e}")
|
111 |
return None, None, None, None
|
|
|
156 |
print(f"Error posting {title}: {e}")
|
157 |
return False
|
158 |
|
159 |
+
def post_blog(doi, title, category, summary, mindmap, citation, uaccess_key, wait_time=5):
|
160 |
if uaccess_key != access_key:
|
161 |
return False
|
162 |
else:
|
163 |
status = True
|
164 |
+
post_title, post_category, post_body, post_image = create_post(doi, title, category, summary, mindmap, citation)
|
165 |
if not all([post_title, post_category, post_body, post_image]):
|
166 |
print(f'Failed to create post {post_title}')
|
167 |
return False
|
|
|
215 |
except:
|
216 |
encoded_bytes = repr(citation)
|
217 |
citation = html.unescape(encoded_bytes)
|
218 |
+
status = post_blog("2412.20276", title, category, summary, mindmap, citation, uaccess_key, 0)
|
219 |
print(status)
|
220 |
return status
|
221 |
|