Skip to content

Commit

Permalink
Merge pull request #20 from ranahaani/main
Browse files Browse the repository at this point in the history
version 0.2.3
  • Loading branch information
ranahaani committed Oct 12, 2021
2 parents 103349d + 49ff97f commit 7ef1870
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
15 changes: 9 additions & 6 deletions gnews/gnews.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

import feedparser
import requests
from bs4 import BeautifulSoup as Soup
from dotenv import load_dotenv

Expand Down Expand Up @@ -89,17 +90,19 @@ def _clean(self, html):
return text

def _process(self, item):
url = item.get("link", "")
url = requests.head(url).headers.get('location', url)
item = {
'title': item.get("title", ""),
'description': self._clean(item.get("description", "")),
'published date': item.get("published", ""),
'url': item.get("link", ""),
'url': url,
'publisher': item.get("source", " ")
}
return item

def get_news(self, key):
if key != '':
if key:
key = "%20".join(key.split(" "))
url = self.BASE_URL + '/search?q={}'.format(key) + self._ceid()
return list(map(self._process, feedparser.parse(url).entries[:self._max_results]))
Expand All @@ -118,13 +121,13 @@ def get_news_by_topic(self, topic: str):
return list(map(self._process, feedparser.parse(url).entries[:self._max_results]))

def get_news_by_location(self, location: str):
if not location:
if location:
url = self.BASE_URL + '/headlines/section/geo/' + location + '?' + self._ceid()
return list(map(self._process, feedparser.parse(url).entries[:self._max_results]))
else:
print("Enter a valid location.")
return []

url = self.BASE_URL + '/headlines/section/geo/' + location + '?' + self._ceid()
return list(map(self._process, feedparser.parse(url).entries[:self._max_results]))

def store_in_mongodb(self, news):
"""MongoDB cluster needs to be created first - https://www.mongodb.com/cloud/atlas/register"""
load_dotenv()
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ bs4~=0.0.1
beautifulsoup4~=4.9.3
pymongo~=3.12.0
dnspython~=1.16.0
python-dotenv~=0.19.0
python-dotenv~=0.19.0
requests==2.26.0
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name='gnews',
version='0.2.2',
version='0.2.3',
author="Muhammad Abdullah",
author_email="ranahaani@gmail.com",
description=' Provide an API to search for articles on Google News and returns a usable JSON response.',
Expand Down

0 comments on commit 7ef1870

Please sign in to comment.