File size: 3,189 Bytes
d82d9bc
 
 
672a01e
d82d9bc
 
 
353e6b2
d82d9bc
 
 
 
943ba68
d82d9bc
 
 
 
 
 
 
 
 
 
 
 
 
 
f4eb7a2
4c413f1
 
 
4b08d28
72c7a0a
 
 
 
 
 
 
 
 
 
4b08d28
 
 
 
d82d9bc
 
 
 
 
 
 
 
 
 
 
 
22c0846
d82d9bc
 
 
ea7c078
 
353e6b2
ea7c078
 
 
 
 
b813dda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import json
import os
import dotenv
import html
from summarize_paper import summarize_paper
from fetch_data import fetch_paper_data_with_category
from post_blog import post_blog
from send_mail import send_email

dotenv.load_dotenv()
access_key = os.getenv("ACCESS_KEY")

def paper_data(paper_data, wait_time=5):
    data = {"status": "success"}
    data['data'] = {}
    paper_data = json.loads(paper_data)
    for category, papers in paper_data.items():
            print(f"Processing category: {category}")
            data['data'][category] = {}
            for paper_id, details in papers.items():
                doi = details.get("doi")
                pdf_url = details.get("pdf_url")
                title = details.get("title")
                citation = details.get("citation")
                if not all([paper_id, doi, pdf_url, title, citation]):
                    print(f"Skipping paper with ID: {paper_id} (missing details)")
                    continue
                summary, mindmap = summarize_paper(pdf_url, paper_id, access_key)
                if not summary or not mindmap:
                    print(f"Skipping paper with ID: {paper_id} (summary/mindmap not found)")
                    continue
                try:
                    try:
                        escaped_title = repr(title.encode('latin1').decode('unicode-escape')).strip()
                    except:
                        escaped_title = repr(title).strip()
                    title = html.escape(str(escaped_title).strip()[1:-1])
                    try:
                        encoded_bytes = citation.encode('latin1').decode('utf-8', errors='replace')
                    except:
                        encoded_bytes = repr(citation)
                    citation = html.unescape(encoded_bytes)
                    post_blog(title, category, summary, mindmap, citation, access_key, wait_time)
                except Exception as e:
                    print(f"Error posting blog '{title}': {e}")
                    continue
                data['data'][category][paper_id] = {
                    "id": paper_id,
                    "doi": doi,
                    "title": title,
                    "category": category,
                    "citation": citation,
                    "summary": summary,
                    "mindmap": mindmap,
                }
    data = json.dumps(data, indent=4, ensure_ascii=False)
    return data

def post_blogpost(uaccess_key, wait_time=5):
    if uaccess_key != access_key:
        return False
    data = fetch_paper_data_with_category(uaccess_key)
    pdata = paper_data(data, wait_time)
    try:
        send_email(pdata)
        print("\n-------------------------------------------------------\nMail Sent\n-------------------------------------------------------\n")
    except Exception as e:
        print(f"\n-------------------------------------------------------\nError sending mail: {e}\n-------------------------------------------------------\n")
    finally:
        print("\n-------------------------------------------------------\nProcess Completed\n-------------------------------------------------------\n")
    return pdata