-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SerpApi GET Method Implementation #113
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
41a7ab2
GET method for SerpApi is added
hakanaktas0 a4150d4
Update tests.py
hakanaktas0 6e062c8
Create django.yml
hakanaktas0 bf689e1
Merge branch 'bounswe:main' into main
hakanaktas0 81c61c6
Update django.yml
hakanaktas0 55eadc5
Update django.yml
hakanaktas0 a3fb46e
Update django.yml
hakanaktas0 edbc837
Update django.yml
hakanaktas0 480c0c1
Update django.yml
hakanaktas0 60d7a49
limited use for SerpApi
hakanaktas0 f4b6302
urls.py modified
hakanaktas0 8d1f9e0
url pattern is updated
hakanaktas0 18010c2
url updated
hakanaktas0 922313a
parameter extraction method changed
hakanaktas0 ae21687
Secret Key is omitted
hakanaktas0 27893c8
tests are enhanced
hakanaktas0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
name: Django CI | ||
|
||
on: | ||
push: | ||
branches: [ "main" ] | ||
pull_request: | ||
branches: [ "main" ] | ||
|
||
env: | ||
serp_api_key: ${{ secrets.SERP_API_KEY }} | ||
SECRET_KEY: ${{ secrets.SECRET_KEY }} | ||
|
||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
defaults: | ||
run: | ||
working-directory: ./practice_app | ||
strategy: | ||
max-parallel: 4 | ||
matrix: | ||
python-version: [ 3.10.11 ] | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Install Dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -r requirements.txt | ||
- name: Run Tests | ||
run: | | ||
python manage.py test |
Empty file.
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,6 @@ | ||
import os | ||
|
||
|
||
api_keys = { | ||
'serp_api' : os.getenv('serp_api_key') | ||
} |
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 |
---|---|---|
@@ -1,3 +1,50 @@ | ||
from django.test import TestCase | ||
|
||
from django.test import TestCase,Client | ||
from unittest import skip | ||
import requests | ||
from . import api_keys | ||
# Create your tests here. | ||
class google_scholar_test_cases(TestCase): | ||
def setUp(self): | ||
self.c = Client() | ||
|
||
|
||
def tearDown(self): | ||
print('GET Tests Completed Successfully') | ||
|
||
def test_404_responses(self): | ||
self.assertEquals(self.c.get("/api/serp-api/?title=").status_code, 404) | ||
self.assertEquals(self.c.get("/api/serp-api/?").status_code, 404) | ||
self.assertEquals(self.c.get("/api/serp-api/").status_code, 404) | ||
self.assertEquals(self.c.get("/api/serp-api/?rows=5").status_code, 404) | ||
self.assertEquals(self.c.get("/api/serp-api/title=sad").status_code, 404) | ||
self.assertEquals(self.c.get("/api/serp-api/?title=&").status_code, 404) | ||
|
||
@skip('limited use for this api') | ||
def test_results(self): | ||
serp_api_response = requests.get('https://serpapi.com/search.json?engine=google_scholar&q=test&hl=en&num=3&api_key=' + api_keys.api_keys['serp_api']) | ||
self.assertEquals(serp_api_response.status_code,200,"SerpApi didn't work as supposed to") | ||
serp_api_response = serp_api_response.json()['organic_results'] | ||
response = self.c.get("/api/google-scholar/?title=test&rows=3") | ||
self.assertEquals(response.status_code,200) | ||
response_content = response.json()['results'] | ||
self.assertEquals(len(response_content),3) | ||
count = 0 | ||
for result in response_content: | ||
self.assertIn('source',result.keys()) | ||
self.assertEquals(result['source'],'google_scholar') | ||
self.assertIn('authors',result.keys()) | ||
self.assertIn('id', result.keys()) | ||
self.assertIn('abstract', result.keys()) | ||
self.assertIn('url', result.keys()) | ||
self.assertIn('pos', result.keys()) | ||
self.assertIn('date', result.keys()) | ||
self.assertIn('title',result.keys()) | ||
self.assertEquals(serp_api_response[count]['title'],result['title']) | ||
self.assertEquals(serp_api_response[count]['link'], result['url']) | ||
self.assertEquals(serp_api_response[count]['position'], result['pos']) | ||
count += 1 | ||
|
||
|
||
|
||
|
||
|
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from django.urls import path | ||
from . import views | ||
|
||
urlpatterns = [ | ||
|
||
path("google-scholar/", views.google_scholar, name="google-scholar"), | ||
] |
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 |
---|---|---|
@@ -1,3 +1,66 @@ | ||
from django.shortcuts import render | ||
import requests | ||
import re | ||
from django.http import JsonResponse | ||
from . import api_keys | ||
|
||
# Create your views here. | ||
|
||
|
||
def google_scholar(request): | ||
number = request.GET.get("rows") | ||
search = request.GET.get("title") | ||
if search == None or search == "": | ||
return JsonResponse({'status': 'Title to search must be given.'}, status=404) | ||
if number == None or number == "" or not number.isnumeric(): | ||
number = 5 | ||
else: | ||
number = int(number) | ||
|
||
request = requests.get('https://serpapi.com/search.json?engine=google_scholar&q=' + search + '&hl=en&num=' + str(number) + '&api_key=' + api_keys.api_keys['serp_api']) | ||
if request.status_code == 200: | ||
request = request.json() | ||
papers = request['organic_results'] | ||
response = {} | ||
results = [] | ||
for paper in papers: | ||
pub_info = paper['publication_info'] | ||
paper_info = {} | ||
paper_info['source'] = 'google_scholar' | ||
|
||
if 'authors' in pub_info.keys(): | ||
defabdullah marked this conversation as resolved.
Show resolved
Hide resolved
|
||
paper_info['authors'] = [] | ||
for author in pub_info['authors']: | ||
a = {'name' : author['name']} | ||
paper_info['authors'].append(a.copy()) | ||
else: | ||
temp = pub_info['summary'].split('-')[0].strip() | ||
temp = temp.split(',') | ||
authors = [] | ||
for i in temp: | ||
i = i.strip() | ||
author = {} | ||
author['name'] = i | ||
authors.append(author.copy()) | ||
paper_info['authors'] = authors | ||
|
||
paper_info['id'] = paper['result_id'] | ||
summary = pub_info['summary'] | ||
summary = '-' + summary + '-' | ||
year = re.findall('[\W|\s](\d{4})[\W|\s]', summary) | ||
if len(year) == 0: | ||
year = None | ||
paper_info['date'] = 'Not found' | ||
else: | ||
year = int(year[0]) | ||
paper_info['date'] = year | ||
|
||
paper_info['abstract'] = paper['snippet'] | ||
paper_info['title'] = paper['title'] | ||
paper_info['url'] = paper['link'] | ||
paper_info['pos'] = paper['position'] | ||
results.append(paper_info.copy()) | ||
response['results'] = results | ||
return JsonResponse(response) | ||
elif request.status_code == 404: | ||
return JsonResponse({'status': 'Unsuccessful Search.'}, status=404) | ||
else: | ||
return JsonResponse({'status': 'An internal server error has occured. Please try again.'},status=503) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Django==4.2 | ||
requests==2.29.0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, I was just looking at this code again and I noticed that the class naming doesn't quite align with our decision to use PEP-8 Style, see class naming convention. Just a friendly reminder for future reference!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thx for the heads up. I will fix them in a future PR.