2023-10-10 10:25:04 +08:00

100 lines
2.8 KiB
Python

import re
import jwt
import time
import json
import requests
from scholarly import scholarly
from scholarly import ProxyGenerator
# Aminer secret key
secret_key = "KJlS8eT9y9Gc1w=="
user_id = "650c31aa078ed986b5d526cc"
expire_time = int(time.time()) + 60 # Expire in 1 min
now_time = int(time.time())
# Aminer JWT token generator
head = {
"alg": "HS256",
"sign_type": "SIGN"
}
payload = {
"user_id": user_id,
"exp": expire_time,
"timestamp": now_time
}
jwt_token = jwt.encode(payload, secret_key, algorithm="HS256", headers=head)
# Aminer API
api_get_id = "https://datacenter.aminer.cn/gateway/open_platform/api/v3/paper/list/by/publish"
api_get_citation = "https://datacenter.aminer.cn/gateway/open_platform/api/v3/paper/detail/list"
def aminer_get_id(title):
headers = {
"Authorization": f"Bearer {jwt_token}"
}
params = {
"page": "",
"size": "",
"title": re.sub(r'[^a-zA-Z0-9\s]+', ' ', title).strip()
}
response = requests.get(api_get_id, headers=headers, params=params)
if response.status_code == 200:
data = response.json()
if data.get("success") and data['data'] is not None:
aminer_paper_id.append(data['data'][0]['id'])
else:
not_on_aminer.append(title)
def aminer_post_citation(aminer_id):
headers = {
"Content-Type": "application/json;charset=utf-8",
"Authorization": f"Bearer {jwt_token}"
}
request_data = {
"ids": aminer_id
}
response = requests.post(api_get_citation, headers=headers, data=json.dumps(request_data))
if response.status_code == 200:
data = response.json()
if data.get("success"):
for item in data.get('data', []):
if 'n_citation' in item:
n_citation = item['n_citation']
else:
n_citation = 0
aminer_paper_citation.append(n_citation)
else:
aminer_paper_citation_retry.append(aminer_id)
def scholarly_get_citation(title):
# # Set up a ProxyGenerator object to use free proxies. This needs to be done only once per session
pg = ProxyGenerator()
pg.FreeProxies()
scholarly.use_proxy(pg)
# Now search Google Scholar from behind a proxy
search_query = scholarly.search_pubs(title)
try:
scholarly.pprint(next(search_query, None))
except StopIteration:
return None
not_on_aminer = []
aminer_paper_id = []
aminer_paper_citation = []
aminer_paper_citation_retry = []
# scholarly_get_citation("Traveling waves for unbalanced bistable equations with density dependent diffusion")
aminer_get_id("Heat kernel estimates for fourth-order non-uniformly elliptic operators with non-strongly convex symbols")
if aminer_paper_id:
aminer_post_citation(aminer_paper_id)
print(aminer_paper_citation)