예제 #1
0
class Directory:
    def __init__(self, manga):
        self.downloadDirectory = "/media/DATA/Linux/personnel/Mangas"
        self.manga = manga
        self.mangaPretty = self.makePretty(manga)
        self.numberImage = 0
        self.console = Console()   
        
    def createDownloadDirectory(self, manga):
        # Create the download directory
        try:
            if (os.path.isdir(self.downloadDirectory) == False):
                os.mkdir(self.downloadDirectory)
            if (os.path.isdir(self.downloadDirectory + self.mangaPretty) == False):
                os.mkdir(self.downloadDirectory + self.mangaPretty)
        except OSError:
            self.console.printError("Unable to create the download directory")
            exit(1)
   
    def makePretty(self, name):
        """Returns the chain given, in order to have a normal name"""
        return str(name).capitalize().replace("/", "").replace("_", " ")

    def chapterDirectoryName(self, number):
        """Returns the name of the directory of the chapter.
           It is useful in order to organize the mass of chapters.
           example:
           500 chaps in the manga,��directory named "001" instead of "1" """
        number = str(number)
        return (mangaNumberLenght - len(number)) * "0" + number


        mangaDirectory = downloadDirectory + self.mangaPretty + "/" + self.chapterDirectoryNname(number)
        if (os.path.isdir(mangaDirectory) == False):
            os.mkdir(mangaDirectory)
        os.chdir(mangaDirectory)



    def lastChapter(self, mangaDirectory):
        """Return the last chapter directory of the manga"""
        return max(float(os.listdir(mangaDirectory)))

    def chapterMissing(self, mangaDirectory):
      """ Returns a list of local missing chapters """
      missing = []
      i = 1
      for chapter in os.listdir(mangaDirectory):
          if chapter != i or len(os.listdir(chapter)) == 0:
              missing = range(i, chapter) + missing
              i = chapter
          i += 1
      return missing



    def mangaDownloaded(self, mangaDirectory):
      """search if the manga directory exist"""
      for manga in os.listdir(downloadDirectory):
          if manga == mangaDirectory:
              return True
      print("The Manga does not exist in the directory!")
      return False
예제 #2
0
class Network:
    def __init__(self):
        # note that Python3 does not read the html code as string
        # but as html code bytearray, convert to string with
        self.maxTries = 5
        self.console = Console()
        print("Initialize Network")

    def getUrlContentUtf8(self, url):
        htmlByteArray = self.getUrlContent(url)
        return htmlByteArray.decode("utf8")

    def getUrlContent(self, url):
        """Returns the content of a webpage at the given url"""
        self.console.printNormal("Donwloading " + url)
        tries = 0
        downloaded = False
        while tries < self.maxTries and downloaded == False:
            try:
                ret = urllib.request.urlopen(url)
                downloaded = True
            except(IOError, socket.error):
                tries += 1
                if tries == self.maxTries:
                    self.console.printError("Maximum tries number reached exiting...")
                    exit(1)
                else:
                    self.console.printError("Failed download, retrying...")
                self.console.printError("Failed download, retrying...")
        self.console.printSuccess("Downloaded!")
        return ret.read()

    def downloadFile(self, url, localName):
        """Retrieves a file"""
        tries = 0
        downloaded = False
        while tries < self.maxTries and downloaded == False:
            try:
                urllib.request.urlretrieve(url, localName)
                downloaded = True
            except HTTPError as e:
                self.console.printError("HTTP Error:", e.code , url)
                tries += 1
            except URLError as e:
                self.console.printError("URL Error:", e.reason , url)
                tries += 1
            if(tries == self.maxTries):
                self.console.printError("Maximum tries number reached exiting...")
                exit(1)
                  
            self.console.printSuccess(url + " downloaded to " + localName)

#net = Network()
#net.downloadFile(http://i13.mangareader.net/7th-period-is-a-secret/2/7th-period-is-a-secret-402446.jpg"), "test.jpg")