aboutsummaryrefslogtreecommitdiff
path: root/plugins/manganelo.py
blob: b19d3e9dbb9f984be0fc5b66578a5e99fe094c5d (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python3

import os
import time
import sys
import requests
import json

from lxml import etree

def usage():
    print("manganelo.py command")
    print("commands:")
    print("  download")
    print("  list")
    exit(1)

def usage_list():
    print("manganelo.py list <url>")
    exit(1)

def usage_download():
    print("manganelo.py download <url> <download_dir>")
    print("examples:")
    print("  manganelo.py download \"https://manganelo.com/chapter/read_naruto_manga_online_free3/chapter_700.5\" /home/adam/Manga/MangaName")
    print("")
    print("Note: The manga directory has to exist.")
    exit(1)

if len(sys.argv) < 2:
    usage()

def download_file(url, save_path):
    file_size = 0
    with requests.get(url, stream=True) as response:
        if not response.ok:
            return 0
        with open(save_path, "wb") as file:
            for chunk in response.iter_content(chunk_size=8192):
                if chunk:
                    file.write(chunk)
                    file_size += len(chunk)
    return file_size

def list_chapters(url, chapter_list_input):
    response = requests.get(url)
    if response.status_code != 200:
        print("Failed to list chapters, server responded with status code %d" % response.status_code)
        exit(2)

    seen_titles = set()
    for item in chapter_list_input:
        title = item.get("title")
        if title and len(title) > 0:
            seen_titles.add(title.lower().replace(" ", ""))

    seen_urls = set()
    for item in chapter_list_input:
        url = item.get("url")
        if url and len(url) > 0:
            seen_urls.add(url.replace("mangakakalot", "manganelo"))

    tree = etree.HTML(response.text)
    chapters = []
    for element in tree.xpath('//ul[@class="row-content-chapter"]//a'):
        element_text = element.text.strip()
        url = element.attrib.get("href").strip()
        if element_text.lower().replace(" ", "") in seen_titles or url in seen_urls:
            break
        chapters.append({ "name": element_text, "url": url })
    print(json.dumps(chapters))

def download_chapter(url, download_dir):
    response = requests.get(url)
    if response.status_code != 200:
        print("Failed to list chapter images, server responded with status code %d" % response.status_code)
        exit(2)

    in_progress_filepath = os.path.join(download_dir, ".in_progress")
    with open(in_progress_filepath, "w") as file:
        file.write(url)

    tree = etree.HTML(response.text)
    img_number = 1
    for image_source in tree.xpath('//div[@class="container-chapter-reader"]/img/@src'):
        ext = image_source[image_source.rfind("."):]
        image_name = str(img_number) + ext
        image_path = os.path.join(download_dir, image_name)
        print("Downloading {} to {}".format(image_source, image_path))
        file_size = download_file(image_source, image_path)
        if file_size < 255:
            try_backup_url = False
            new_image_source = image_source.replace("s3.mkklcdnv3.com", "bu.mkklcdnbuv1.com")
            if new_image_source != image_source:
                try_backup_url = True
            else:
                new_image_source = image_source.replace("s41.mkklcdnv41.com", "bu.mkklcdnbuv1.com")
                try_backup_url = (new_image_source != image_source)

            if try_backup_url:
                file_size = download_file(new_image_source, image_path)
                if file_size < 255:
                    print("resource temporary unavailable: %s" % new_image_source)
                    os.remove(in_progress_filepath)
                    exit(2)
            else:
                print("resource temporary unavailable: %s" % image_source)
                os.remove(in_progress_filepath)
                exit(2)
        img_number += 1
    
    if img_number == 1:
        print("Failed to find images for chapter")
        os.remove(in_progress_filepath)
        exit(2)

    with open(os.path.join(download_dir, ".finished"), "w") as file:
        file.write("1")

    os.remove(in_progress_filepath)

command = sys.argv[1]
if command == "list":
    if len(sys.argv) < 3:
        usage_list()
    
    url = sys.argv[2].replace("mangakakalot", "manganelo")
    chapter_list_input = sys.stdin.read()
    if len(chapter_list_input) == 0:
        chapter_list_input = []
    else:
        chapter_list_input = json.loads(chapter_list_input)
    list_chapters(url, chapter_list_input)
elif command == "download":
    if len(sys.argv) < 4:
        usage_download()
    url = sys.argv[2].replace("mangakakalot", "manganelo")
    download_dir = sys.argv[3]
    download_chapter(url, download_dir)
else:
    usage()