-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo_utils.py
29 lines (22 loc) · 969 Bytes
/
mongo_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import os
import pymongo
class MongoDb:
def __init__(self):
mongodb_string = os.environ.get('MONGO_DB')
self._database = pymongo.MongoClient(mongodb_string)['newspaper']
def insert_article(self, data: dict, language):
collection = self._database[language]
collection.update_one({"url": data["url"]}, {"$set": data}, upsert=True)
def insert_tasks(self, tasks, language):
collection = self._database[language]
for task in tasks:
collection.update_one({"url": task["url"]}, {"$set": task}, upsert=True)
def get_open_task(self, language):
collection = self._database[language]
return collection.find_one({"text": {"$exists": False}})
def get_target_urls(self, language):
collection = self._database["TARGET"]
result = collection.find({'language': language})
if result.count() == 0:
return []
return [i['url'] for i in result]