-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaper-add.py
executable file
·146 lines (130 loc) · 4.27 KB
/
paper-add.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# shebang here
import os
import argparse
import requests
import bs4
import re
from typing import List, Tuple
from dataclasses import dataclass
from notion_client import Client, APIResponseError
notion = Client(auth=os.environ["NOTION_TOKEN"])
paper_db_id = os.environ["PAPER_ADD_PAPERS_ID"]
tags_db_id = os.environ["PAPER_ADD_TAGS_ID"]
@dataclass
class Args:
arxiv_url: str
topics: List[str]
notes: str
def get_title_and_year(arxiv_url: str) -> Tuple[str, str]:
soup = bs4.BeautifulSoup(requests.get(arxiv_url).text, features="html.parser")
title = soup.find_all("h1", {
"class": ["title", "mathjax"]
})[0].contents[-1]
regex = re.compile(r"(\d{4})")
date = regex.search(soup.find_all("div", {
"class": ["dateline"]
})[0].contents[0].strip()).group(0)
return title, date
def get_or_create_rel_id(rel_name: str) -> str:
rel = notion.databases.query(tags_db_id, **{
"filter": {
"property": "Name",
"title": {
"equals": rel_name
}
}
})
if not rel["results"]:
try:
print(f"Creating new topic \"{rel_name}\"...")
notion.pages.create(
**{
"parent": {
"database_id": tags_db_id,
},
"properties": {
"Name": [
{
"text": {
"content": rel_name
}
}
]
}
}
)
rel = notion.databases.query(tags_db_id, **{
"filter": {
"property": "Name",
"title": {
"equals": rel_name
}
}
})
except Exception as e:
print(f"Could not create new topic \"{rel_name}\":")
print(e)
return rel["results"][0]["id"]
def get_rel_ids(rel_names: List[str]) -> List[str]:
rels = [get_or_create_rel_id(rel_name) for rel_name in rel_names]
return rels
def main(args: Args) -> None:
arxiv_url = args.arxiv_url
topics = args.topics
notes = args.notes
rel_list = [{"id": rel_id} for rel_id in get_rel_ids(topics)]
try:
title, year = get_title_and_year(arxiv_url)
obj = {"parent": {
"database_id": paper_db_id
},
"properties": {
"Name": {
"title": [
{
"text": {
"content": title
}
}
]
},
"Year": {
"rich_text": [
{
"text": {
"content": year
}
}
]
},
"Link": {
"url": arxiv_url
},
"Notes": {
"rich_text": [
{
"text": {
"content": notes
}
}
]
},
"Read": {
"checkbox": False
},
"Topics": {
"relation": rel_list
},
}
}
notion.pages.create(**obj)
except APIResponseError as error:
print(error)
exit()
print("Success!")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--arxiv_url", type=str, required=True, help="URL of the arXiv paper")
parser.add_argument("--topics", nargs="+", required=True, help="List of topics to put the paper under")
parser.add_argument("--notes", type=str, required=True, help="Any miscellaneous notes to add")
main(Args(**vars(parser.parse_args())))