-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbClient.py
70 lines (51 loc) · 2.4 KB
/
dbClient.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# databaseInserter.py - This file inserts all the img URLS (collected by <scraper.py>) on MongoDB-Atlas.
from environs import Env
from pymongo import MongoClient
from pymongo.server_api import ServerApi
class Client:
def __init__(self) -> None:
# Reading the .env file.
env = Env()
env.read_env()
# Getting the username and password of MongoDB-Atlas User from the .env-file.
username = env.str("atlas_username")
password = env.str("atlas_user_password")
# Creating a client-Obj for the Atlas-Cluster.
client = MongoClient(
f"mongodb+srv://{username}:{password}@maincluster.bwozlbo.mongodb.net/?retryWrites=true&w=majority",
server_api=ServerApi("1"),
)
# Creating a Database inside the cluster.
self.Database = client["MangaURLs"]
def insert(self, mangaName, URLData) -> None:
print("\nInsertion Began.\n")
# Creating a collection for the Manga in the DataBase.
collection = self.Database[mangaName]
# The list of all the chapter documents.
chapters = []
# Looping through the URLData and adding it to the Collection.
for chapter in URLData:
# Getting the chapterImgURLs in appropriate format for insertion in some steps.
# 1 - The local Chapter Document.
chapterDict = {}
# 2 - A default array in the local-Document.
chapterDict.setdefault("ChapterNum", str(chapter[0]))
chapterDict.setdefault(f"pages", [])
# 3 - Adding all the imgURLs in the local-Document.
for page in chapter[1:]:
chapterDict[f"pages"].append(page)
# Appending this chapter document to the list of chapters.
chapters.append(chapterDict)
# Inserting the documents to the Database.
collection.insert_many(chapters)
print("Insertion Ended.")
def drop(self, mangaName) -> None:
# Checking if there isn't a collection for the given manga in the database.
if mangaName not in self.Database.list_collection_names():
print("You don't have a collection for %s, in the database." % mangaName)
return None
# Selecting the collection.
collection = self.Database[mangaName]
# Droping the collection.
collection.drop()
print("%s Collection Dropped." % mangaName)