-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a publication panel for each edge.
- Loading branch information
Showing
11 changed files
with
449 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
pub mod route; | ||
pub mod schema; | ||
pub mod auth; | ||
pub mod auth; | ||
pub mod req; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
use anyhow; | ||
use poem_openapi::{types::ToJSON, Object}; | ||
use reqwest; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
const CONSENSUS_API: &str = "https://consensus.app/api/paper_search/"; | ||
const CONSENSUS_DETAIL_API: &str = "https://consensus.app/api/papers/details/"; | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Object)] | ||
pub struct PublicationRecords { | ||
pub records: Vec<Publication>, | ||
pub total: u64, | ||
pub page: u64, | ||
pub page_size: u64, | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Object)] | ||
pub struct Publication { | ||
pub authors: Vec<String>, | ||
pub citation_count: Option<u64>, | ||
pub summary: String, | ||
pub journal: String, | ||
pub title: String, | ||
pub year: Option<u64>, | ||
pub doc_id: String, | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Object)] | ||
pub struct PublicationDetail { | ||
pub authors: Vec<String>, | ||
pub citation_count: Option<u64>, | ||
pub summary: String, | ||
pub journal: String, | ||
pub title: String, | ||
pub year: Option<u64>, | ||
pub doc_id: String, | ||
pub article_abstract: Option<String>, | ||
pub doi: Option<String>, | ||
pub provider_url: Option<String>, | ||
} | ||
|
||
impl Publication { | ||
pub async fn fetch_publication( | ||
id: &str, | ||
) -> Result<PublicationDetail, anyhow::Error> { | ||
let api_token = match std::env::var("CONSENSUS_API_TOKEN") { | ||
Ok(token) => token, | ||
Err(_) => { | ||
return Err(anyhow::anyhow!("CONSENSUS_API_TOKEN not found")); | ||
} | ||
}; | ||
|
||
let url = format!("{}{}", CONSENSUS_DETAIL_API, id); | ||
let cookie = format!("_session={}", api_token); | ||
let client = reqwest::Client::new(); | ||
let res = client.get(&url).header("Cookie", cookie).send().await?; | ||
|
||
if res.status().is_success() { | ||
let body = res.text().await?; | ||
let json: serde_json::Value = serde_json::from_str(&body)?; | ||
let authors = json["authors"].as_array().unwrap(); | ||
let mut authors_vec = Vec::new(); | ||
for author in authors { | ||
authors_vec.push(author.as_str().unwrap().to_string()); | ||
} | ||
let citation_count = json["citation_count"].as_u64(); | ||
let summary = json["abstract_takeaway"].as_str().unwrap().to_string(); | ||
// Such as { "journal": { "title": "The Journal of biological chemistry","scimago_quartile": 1 }} | ||
let journal = json["journal"]["title"].as_str().unwrap().to_string(); | ||
let title = json["title"].as_str().unwrap().to_string(); | ||
let year = json["year"].as_u64(); | ||
let doc_id = json["id"].as_str().unwrap().to_string(); | ||
let article_abstract = json["abstract"].as_str().map(|s| s.to_string()); | ||
let doi = json["doi"].as_str().map(|s| s.to_string()); | ||
let provider_url = json["provider_url"].as_str().map(|s| s.to_string()); | ||
|
||
Ok(PublicationDetail { | ||
authors: authors_vec, | ||
citation_count: citation_count, | ||
summary: summary, | ||
journal: journal, | ||
title: title, | ||
year: year, | ||
doc_id: doc_id, | ||
article_abstract: article_abstract, | ||
doi: doi, | ||
provider_url: provider_url, | ||
}) | ||
} else { | ||
Err(anyhow::anyhow!("Failed to fetch publication")) | ||
} | ||
} | ||
|
||
pub async fn fetch_publications( | ||
query_str: &str, | ||
page: Option<u64>, | ||
page_size: Option<u64>, | ||
) -> Result<PublicationRecords, anyhow::Error> { | ||
let api_token = match std::env::var("CONSENSUS_API_TOKEN") { | ||
Ok(token) => token, | ||
Err(_) => { | ||
return Err(anyhow::anyhow!("CONSENSUS_API_TOKEN not found")); | ||
} | ||
}; | ||
|
||
// We only need to fetch the top 10 results currently. | ||
let total = 10; | ||
let page = 0; | ||
let page_size = 10; | ||
|
||
let mut records = Vec::new(); | ||
let url = format!( | ||
"{}?query={}&page={}&size={}", | ||
CONSENSUS_API, query_str, page, page_size | ||
); | ||
let cookie = format!("_session={}", api_token); | ||
let client = reqwest::Client::new(); | ||
let res = client.get(&url).header("Cookie", cookie).send().await?; | ||
|
||
if res.status().is_success() { | ||
let body = res.text().await?; | ||
let json: serde_json::Value = serde_json::from_str(&body)?; | ||
let items = json["papers"].as_array().unwrap(); | ||
for item in items { | ||
let authors = item["authors"].as_array().unwrap(); | ||
let mut authors_vec = Vec::new(); | ||
for author in authors { | ||
authors_vec.push(author.as_str().unwrap().to_string()); | ||
} | ||
let citation_count = item["citation_count"].as_u64(); | ||
let summary = item["display_text"].as_str().unwrap().to_string(); | ||
let journal = item["journal"].as_str().unwrap().to_string(); | ||
let title = item["title"].as_str().unwrap().to_string(); | ||
let year = item["year"].as_u64(); | ||
let doc_id = item["doc_id"].as_str().unwrap().to_string(); | ||
records.push(Publication { | ||
authors: authors_vec, | ||
citation_count: citation_count, | ||
summary: summary, | ||
journal: journal, | ||
title: title, | ||
year: year, | ||
doc_id: doc_id, | ||
}); | ||
} | ||
} | ||
|
||
Ok(PublicationRecords { | ||
records: records, | ||
total: total, | ||
page: page, | ||
page_size: page_size, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.