import json
import shutil
import glob

from os import makedirs
from os.path import isdir
from datetime import datetime


class DBmanage:
    def __init__(self):
        self.databasePath = './database/'
        self.subFolderPath = self.databasePath + self.getCrawlDate() + '/'
        self.checkAndCreateDatabaseFolder()

    def getDBPath(self):
        return self.databasePath

    def getSubFolderPath(self):
        return self.subFolderPath

    def checkAndCreateDatabaseFolder(self):
        if not isdir(self.databasePath):
            print('Database folder not exists!')
            makedirs(self.databasePath)
            print('Created database.')

        if not isdir(self.subFolderPath):
            print('Sub-folder not exists!')
            makedirs(self.subFolderPath)
            print('Created subfolder at', self.subFolderPath)

    def updateDatabase(self):
        pass

    def removeDatabase(self):
        shutil.rmtree(self.databasePath)
        print('Database removed.')

    def saveResultFile(self, crawlResult, crawlResultFilePath):
        with open(crawlResultFilePath, 'w', encoding='utf-8') as f:
            json.dump(crawlResult, f, sort_keys=True,
                      indent=4, ensure_ascii=False)
            print('Crawl result saved at:', crawlResultFilePath)
            print()

    def getCrawlDate(self):
        return datetime.now().strftime('%Y%m%d')

    def saveCrawledBoardResult(self, crawlResult):
        boardName = crawlResult['boardName']
        crawlResultFilePath = self.getBoardResultFilePath(boardName)
        self.saveResultFile(crawlResult, crawlResultFilePath)

    def getBoardResultFilePath(self, boardName):
        crawlDate = self.getCrawlDate()
        crawlResultText = 'boardResult' + crawlDate
        crawlResultFileName = crawlResultText + '_' + boardName + '.json'
        return self.subFolderPath + crawlResultFileName

    def saveCrawledArticleResult(self, crawlResult):
        boardName = crawlResult['boardName']
        crawlResultFilePath = self.getArticleResultFilePath(boardName)
        self.saveResultFile(crawlResult, crawlResultFilePath)

    def getArticleResultFilePath(self, boardName):
        crawlDate = self.getCrawlDate()
        crawlResultText = 'articleResult' + crawlDate
        crawlResultFileName = crawlResultText + '_' + boardName + '.json'
        return self.subFolderPath + crawlResultFileName

    def loadResultFile(self, path):
        with open(path, encoding='utf8') as f:
            result = json.load(f)
        return result

    def loadCrawledBoardResult(self, boardResultFilePath):
        return self.loadResultFile(boardResultFilePath)

    def loadCrawledArticleResult(self, articleResultFilePath):
        return self.loadResultFile(articleResultFilePath)

    def getLatestBoardResultPath(self, boardName):
        pattern = self.subFolderPath + 'boardResult*' + boardName + '.json'
        return glob.glob(pattern)[-1]

@@ -652,24 +652,63 @@
 atabase.')%0A%0A
+    def checkAndCreateSubFolder(self):%0A
         if n
@@ -733,24 +733,24 @@
 olderPath):%0A
-
             
@@ -889,52 +889,8 @@
 h)%0A%0A
-    def updateDatabase(self):%0A        pass%0A%0A
     
@@ -988,24 +988,24 @@
 removed.')%0A%0A
-
     def save
@@ -1048,32 +1048,72 @@
 esultFilePath):%0A
+        self.checkAndCreateSubFolder()%0A%0A
         with ope

add checkAndCreateSubFolder()