Skip to content

Commit b3d7b19

Browse files
authored
Merge pull request #92 from ranahaani/feat/remove-mongodb-and-update-requests
remove mongodb and fix requests
2 parents f0947c7 + b73510f commit b3d7b19

File tree

3 files changed

+2
-82
lines changed

3 files changed

+2
-82
lines changed

gnews/gnews.py

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
import logging
2-
import os
3-
import sys
42
import urllib.request
53
import datetime
64
import inspect
75
import warnings
86

97
import feedparser
108
from bs4 import BeautifulSoup as Soup
11-
from dotenv import load_dotenv
12-
13-
try:
14-
import newspaper # Optional - required by GNews.get_full_article()
15-
except ImportError:
16-
pass
179

1810
from gnews.utils.constants import AVAILABLE_COUNTRIES, AVAILABLE_LANGUAGES, TOPICS, BASE_URL, USER_AGENT
19-
from gnews.utils.utils import connect_database, post_database, process_url
11+
from gnews.utils.utils import process_url
2012

2113
logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO,
2214
datefmt='%m/%d/%Y %I:%M:%S %p')
@@ -312,22 +304,3 @@ def _get_news(self, query):
312304
except Exception as err:
313305
logger.error(err.args[0])
314306
return []
315-
316-
def store_in_mongodb(self, news):
317-
"""
318-
- MongoDB cluster needs to be created first - https://www.mongodb.com/cloud/atlas/register
319-
- Connect to the MongoDB cluster
320-
- Create a new collection
321-
- Insert the news into the collection
322-
:param news: the news object that we created in the previous function
323-
"""
324-
325-
load_dotenv()
326-
327-
db_user = os.getenv("DB_USER")
328-
db_pw = os.getenv("DB_PW")
329-
db_name = os.getenv("DB_NAME")
330-
collection_name = os.getenv("COLLECTION_NAME")
331-
332-
collection = connect_database(db_user, db_pw, db_name, collection_name)
333-
post_database(collection, news)

gnews/utils/utils.py

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
import logging
44
import re
55

6-
import pymongo
76
import requests
87
from gnews.utils.constants import AVAILABLE_COUNTRIES, AVAILABLE_LANGUAGES, GOOGLE_NEWS_REGEX
9-
from pymongo import MongoClient
108

119

1210
def lang_mapping(lang):
@@ -17,55 +15,6 @@ def country_mapping(country):
1715
return AVAILABLE_COUNTRIES.get(country)
1816

1917

20-
def connect_database(db_user, db_pw, db_name, collection_name):
21-
"""Mongo DB Establish Cluster Connection"""
22-
23-
# .env file Structure:
24-
25-
# DB_USER="..."
26-
# DB_PW="..."
27-
# DB_NAME="..."
28-
# COLLECTION_NAME="..."
29-
30-
# name of the mongodb cluster as well as the database name should be "gnews"
31-
32-
try:
33-
cluster = MongoClient(
34-
"mongodb+srv://" +
35-
db_user +
36-
":" +
37-
db_pw +
38-
"@gnews.stjap.mongodb.net/" +
39-
db_name +
40-
"?retryWrites=true&w=majority"
41-
)
42-
43-
db = cluster[db_name]
44-
collection = db[collection_name]
45-
46-
return collection
47-
48-
except Exception as e:
49-
print("Connection Error.", e)
50-
51-
52-
def post_database(collection, news):
53-
"""post unique news articles to mongodb database"""
54-
doc = {
55-
"_id": hashlib.sha256(str(json.dumps(news)).encode('utf-8')).hexdigest(),
56-
"title": news['title'],
57-
"description": news['description'],
58-
"published_date": news['published date'],
59-
"url": news['url'],
60-
"publisher": news['publisher']
61-
}
62-
63-
try:
64-
collection.update_one(doc, {'$set': doc}, upsert=True)
65-
except pymongo.errors.DuplicateKeyError:
66-
logging.error("Posting to database failed.")
67-
68-
6918
def process_url(item, exclude_websites):
7019
source = item.get('source').get('href')
7120
if not all([not re.match(website, source) for website in

requirements.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
feedparser~=6.0.2
22
beautifulsoup4>=4.9.3,<5
3-
pymongo~=3.12.0
43
dnspython~=1.16.0
5-
python-dotenv>=0.19.0
6-
requests>=2.26.0,<3
4+
requests

0 commit comments

Comments
 (0)