-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_script.py
124 lines (110 loc) · 4.35 KB
/
flask_script.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
from flask import Flask, render_template_string, request
import requests
app = Flask(__name__)
# Function to get metadata using CrossRef API
def get_paper_title(doi):
url = f"https://api.crossref.org/works/{doi}"
response = requests.get(url)
if response.status_code == 200:
metadata = response.json()
return metadata['message']['title'][0] # Extracting the paper title
return None
# Function to fetch package info from Poseidon API
def fetch_poseidon_packages(archive_name):
url = f"https://server.poseidon-adna.org/packages?archive={archive_name}"
response = requests.get(url)
if response.status_code == 200:
return response.json().get("serverResponse", {}).get("packageInfo", [])
return []
# Parse the package description
def clean_description(description):
return description.split('.')[0] + '.' # Remove the part after the first full stop
# Check if a paper title is in the archive
def check_paper_in_archive(paper_title, package_info_list):
for package in package_info_list:
cleaned_description = clean_description(package['description'])
if paper_title.lower() in cleaned_description.lower():
return True # Paper title matches the cleaned description
return False
# Preprocess DOIs to ensure they are in the correct format
def preprocess_doi(doi):
return doi.replace("https://doi.org/", "").strip()
# Main route for displaying and processing the form
@app.route('/', methods=['GET', 'POST'])
def index():
result = []
if request.method == 'POST':
# Get DOIs from the form and preprocess them
raw_dois = request.form['dois']
dois = [preprocess_doi(doi) for doi in raw_dois.split(',')]
# Archive names
archives = {
'community': 'community-archive',
'minotaur': 'minotaur-archive',
'aadr': 'aadr-archive'
}
# Fetch package info for all archives
package_info_by_archive = {
archive: fetch_poseidon_packages(archives[archive])
for archive in archives
}
# Process each DOI
for doi in dois:
paper_title = get_paper_title(doi)
if paper_title:
paper = {
'doi': doi,
'title': paper_title,
'community_archive': check_paper_in_archive(paper_title, package_info_by_archive['community']),
'aadr_archive': check_paper_in_archive(paper_title, package_info_by_archive['aadr']),
'minotaur_archive': check_paper_in_archive(paper_title, package_info_by_archive['minotaur'])
}
result.append(paper)
# HTML template for rendering the form and results
html_template = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Paper Directory Check</title>
<style>
table { width: 100%; border-collapse: collapse; }
th, td { padding: 8px; border: 1px solid #ddd; text-align: left; }
th { background-color: #f2f2f2; }
</style>
</head>
<body>
<h1>Paper Directory Check</h1>
<form method="POST">
<label for="dois">Enter DOIs (comma-separated):</label><br>
<input type="text" id="dois" name="dois" style="width: 100%; padding: 8px;"><br><br>
<button type="submit">Check</button>
</form>
{% if result %}
<h2>Results:</h2>
<table>
<tr>
<th>DOI</th>
<th>Title</th>
<th>Community Archive</th>
<th>AADR Archive</th>
<th>Minotaur Archive</th>
</tr>
{% for paper in result %}
<tr>
<td>{{ paper.doi }}</td>
<td>{{ paper.title }}</td>
<td>{{ '✔' if paper.community_archive else '✘' }}</td>
<td>{{ '✔' if paper.aadr_archive else '✘' }}</td>
<td>{{ '✔' if paper.minotaur_archive else '✘' }}</td>
</tr>
{% endfor %}
</table>
{% endif %}
</body>
</html>
"""
return render_template_string(html_template, result=result)
if __name__ == '__main__':
app.run(debug=True)