Skip to content

Instantly share code, notes, and snippets.

@ilyes-d
Created June 3, 2022 23:54
Show Gist options
  • Save ilyes-d/8cbfefa44c6e820de1f2a096a7e9d493 to your computer and use it in GitHub Desktop.
Save ilyes-d/8cbfefa44c6e820de1f2a096a7e9d493 to your computer and use it in GitHub Desktop.
functions
from django.shortcuts import render
from serpapi import GoogleSearch
from dotenv import load_dotenv
import os
load_dotenv()
api_key = os.getenv("api_key")
def index(request):
return render(request,'index.html')
def gs_params(author_id):
params = {
"engine": "google_scholar_author",
"author_id": author_id,
"api_key": api_key
}
return params
def get_author_profile(request):
if request.method == 'GET':
context = {}
author_url = request.GET.get('author_url')
author_id = str(author_url).partition('user=')[2][:12]
params = gs_params(author_id)
search = GoogleSearch(params)
results = search.get_dict()
if 'author' in results :
context["name"] = results["author"]["name"]
context["total_citations"] = results["cited_by"]["table"][0]["citations"]["all"]
else:
context["errors"] = {
"invalid author url",
"your search for the month is exhausted",
"or your api key is invalid. Your API key should be here: https://serpapi.com/manage-api-key"
}
return render(request,'index.html', context)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment