Skip to content

Commit 62d8184

Browse files
committed
Merge branch 'export-endpoint' into interface-changes-export-new
2 parents 8126503 + 6584261 commit 62d8184

6 files changed

Lines changed: 150 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ More information can be found in the following papers:
9292

9393
Kraker, P., Schramm, M., Kittel, C., Chamberlain, S., & Arrow, T. (2018). [VIPER: The Visual Project Explorer](https://zenodo.org/record/1248119). Zenodo. doi:10.5281/zenodo.2587129
9494

95-
Kraker, P., Kittel, C., & Enkhbayar, A. (2016). [Open Knowledge Maps: Creating a Visual Interface to the World’s Scientific Knowledge Based on Natural Language Processing](http://0277.ch/ojs/index.php/cdrs_0277/article/view/157/355). 027.7 Journal for Library Culture, 4(2), 98–103. doi:10.12685/027.7-4-2-157
95+
Kraker, P., Kittel, C., & Enkhbayar, A. (2016). [Open Knowledge Maps: Creating a Visual Interface to the World’s Scientific Knowledge Based on Natural Language Processing](https://doi.org/10.12685/027.7-4-2-157). 027.7 Journal for Library Culture, 4(2), 98–103. doi:10.12685/027.7-4-2-157
9696

9797
Kraker, P., Schlögl, C. , Jack, K. & Lindstaedt, S. (2015). [Visualization of Co-Readership Patterns from an Online Reference Management System](http://arxiv.org/abs/1409.0348). Journal of Informetrics, 9(1), 169–182. doi:10.1016/j.joi.2014.12.003
9898

server/preprocessing/other-scripts/base.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ get_papers <- function(query, params, limit=100,
140140
subject_cleaned = gsub(" ?\\d[:?-?]?(\\d+.)+", "", subject_cleaned) # replace residuals like 5:621.313.323 or '5-76.95'
141141
subject_cleaned = gsub("\\w+:\\w+-(\\w+\\/)+", "", subject_cleaned) # replace residuals like Info:eu-repo/classification/
142142
subject_cleaned = gsub("^; $", "", subject_cleaned) # replace residuals like Info:eu-repo/classification/
143+
subject_cleaned = gsub(",", ", ", subject_cleaned) # clean up keyword separation
144+
subject_cleaned = gsub("\\s+", " ", subject_cleaned) # clean up keyword separation
143145

144146
metadata$subject = subject_cleaned
145147

server/services/exportMetadata.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
require_once dirname(__FILE__) . '/../classes/headstart/library/CommUtils.php';
4+
require_once dirname(__FILE__) . '/../classes/headstart/library/APIClient.php';
5+
require_once dirname(__FILE__) . '/../classes/headstart/library/toolkit.php';
6+
use headstart\library;
7+
8+
function export($export_format, $metadata_json) {
9+
$INI_DIR = dirname(__FILE__) . "/../preprocessing/conf/";
10+
$ini_array = library\Toolkit::loadIni($INI_DIR);
11+
$apiclient = new \headstart\library\APIClient($ini_array);
12+
13+
14+
$payload = $metadata_json;
15+
#$res = $apiclient->call_persistence("export/" . $export_format, $payload);
16+
$res = $apiclient->call_api("export/" . $export_format, $payload);
17+
return $res;
18+
};
19+
20+
$json = file_get_contents('php://input');
21+
$format = (isset($_REQUEST['format'])) ? $_REQUEST['format'] : "bibtex";
22+
$download = (isset($_REQUEST['download'])) ? $_REQUEST['download'] : false;
23+
$result = export($format, $json);
24+
25+
if (isset($result["status"]) && $result["status"] === "error") {
26+
header('Content-type: application/json');
27+
echo json_encode($result);
28+
}
29+
30+
if (isset($download) & $download==true ) {
31+
header('Content-type: application/text');
32+
header('Content-Disposition: attachment; filename=metadata.' . $format);
33+
} else {
34+
header('Content-type: text/plain');
35+
}
36+
37+
// $origin = $_SERVER['HTTP_ORIGIN'];
38+
// $allowed_domains = [
39+
// 'http://openknowledgemaps.org',
40+
// 'https://openknowledgemaps.org',
41+
// 'http://dev.openknowledgemaps.org',
42+
// 'https://dev.openknowledgemaps.org'
43+
// ];
44+
45+
// if (in_array($origin, $allowed_domains)) {
46+
// header('Access-Control-Allow-Origin: ' . $origin);
47+
// }
48+
49+
$result = json_decode($result["result"], true);
50+
echo $result["export"];
51+
52+
?>

server/workers/api/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ pandas
1212
pyyaml
1313
flasgger
1414
psycopg2-binary
15+
bibtexparser
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import os
2+
3+
from flask import Blueprint, request, make_response, jsonify, abort
4+
from flask_restx import Namespace, Resource, fields
5+
from bibtexparser.bwriter import BibTexWriter
6+
from bibtexparser.bibdatabase import BibDatabase
7+
8+
export_ns = Namespace("export", description="metadata export API operations")
9+
10+
def transform2bibtex(metadata):
11+
# TODO: add mapping from resulttype to ARTICLETYPE
12+
# possible published_in parser
13+
# field renaming, e.g. paper_abstract to abstract
14+
# choose correct fields, e.g. author_string for author
15+
# use different field for ID
16+
title = metadata.get("title", "")
17+
author = metadata.get("authors", "")
18+
abstract = metadata.get("abstract", "")
19+
year = metadata.get("year", "")
20+
doi = metadata.get("doi", "")
21+
id = metadata.get("id", "")
22+
published_in = metadata.get("published_in", "")
23+
fields = {
24+
"title": title,
25+
"author": author,
26+
"abstract": abstract,
27+
"year": year,
28+
"doi": doi,
29+
"published_in": published_in,
30+
"ENTRYTYPE": "article",
31+
"ID": id
32+
}
33+
db = BibDatabase()
34+
writer = BibTexWriter()
35+
db.entries.append(fields)
36+
export = writer.write(db)
37+
result = {
38+
"format": "bibtex",
39+
"export": export
40+
}
41+
return result
42+
43+
def parse_published_in(published_in):
44+
pass
45+
46+
def transform2ris(metadata):
47+
pass
48+
49+
@export_ns.route('/<format>')
50+
class exportMetadata(Resource):
51+
52+
def post(self, format):
53+
try:
54+
metadata = request.json
55+
export_ns.logger.debug(metadata)
56+
if format == "bibtex":
57+
result = transform2bibtex(metadata)
58+
headers = {'ContentType': 'application/text'}
59+
code = 200
60+
elif format == "ris":
61+
result = transform2ris(metadata)
62+
headers = {'ContentType': 'application/text'}
63+
code = 200
64+
else:
65+
result = {"status": "error",
66+
"reason": "output format not recognized, must bei either bibtex or ris"}
67+
code = 400
68+
export_ns.logger.debug(result)
69+
headers = {'ContentType': 'application/json'}
70+
return make_response(jsonify(result),
71+
code,
72+
headers)
73+
except Exception as e:
74+
export_ns.logger.error(e)
75+
result = {'success': False, 'reason': e}
76+
headers = {'ContentType': 'application/json'}
77+
return make_response(jsonify(result),
78+
500,
79+
headers)
80+
81+
82+
@export_ns.route('/service_version')
83+
class ServiceVersion(Resource):
84+
def get(self):
85+
result = {"service_version": os.getenv("SERVICE_VERSION")}
86+
return make_response(result, 200, {"Content-Type": "application/json"})
87+
88+
@export_ns.route('/healthcheck')
89+
class Healthcheck(Resource):
90+
def get(self):
91+
result = {"status": "I'm good"}
92+
return make_response(result, 200, {"Content-Type": "application/json"})

server/workers/api/src/app.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from apis.pubmed import pubmed_ns
1212
from apis.openaire import openaire_ns
1313
from apis.create_vis import vis_ns
14+
from apis.export import export_ns
1415

1516
from utils.monkeypatches import ReverseProxied, __schema__, specs_url, _register_apidoc
1617
import logging
@@ -47,6 +48,7 @@ def api_patches(app):
4748
api.add_namespace(pubmed_ns, path='/pubmed')
4849
api.add_namespace(openaire_ns, path='/openaire')
4950
api.add_namespace(vis_ns, path='/vis')
51+
api.add_namespace(export_ns, path='/export')
5052

5153
app.logger.debug(app.config)
5254
app.logger.debug(app.url_map)

0 commit comments

Comments
 (0)