aboutsummaryrefslogtreecommitdiffstats
path: root/kaminokumo
blob: 2b75181a566ce3fd1781e35560896ef7de043f84 (plain) (blame)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/python3

import argparse
import json
import sys
import os
import requests
import re
import concurrent.futures
import bs4
import pathlib


class bcolors:
    HEADER = "\033[95m"
    OKBLUE = "\033[94m"
    OKCYAN = "\033[96m"
    OKGREEN = "\033[92m"
    WARNING = "\033[93m"
    FAIL = "\033[91m"
    ENDC = "\033[0m"
    BOLD = "\033[1m"
    UNDERLINE = "\033[4m"


green = bcolors.OKGREEN if sys.stdout.isatty() else ""
blue = bcolors.OKBLUE if sys.stdout.isatty() else ""
cyan = bcolors.OKCYAN if sys.stdout.isatty() else ""


class Argparser(object):
    def __init__(self):
        parser = argparse.ArgumentParser()
        parser.add_argument("--url", type=str, nargs="+", help="url to scrape")
        parser.add_argument(
            "--name", type=str, nargs="+", help="url to scrape"
        )
        parser.add_argument(
            "--dbg", action="store_true", help="debug", default=False
        )
        parser.add_argument(
            "--demon", action="store_true", help="run as daemon", default=False
        )
        parser.add_argument(
            "--manga",
            action="store_true",
            help="run manga scraper",
            default=False,
        )
        parser.add_argument(
            "--anime",
            action="store_true",
            help="run anime scraper",
            default=False,
        )
        parser.add_argument(
            "--cb", action="store_true", help="run cb scraper", default=False
        )
        parser.add_argument(
            "--slumber", type=int, help="how long the demon sleeps", default=60
        )
        self.args = parser.parse_args()


path = str()
if pathlib.Path(sys.argv[0]).is_symlink():
    path = os.readlink(sys.argv[0])
else:
    path = sys.argv[0]


def single_get(url: str) -> requests.Response:
    return requests.get(url, allow_redirects=True)


def multi_get(urls: list) -> list:
    with concurrent.futures.ThreadPoolExecutor(max_workers=10) as pool:
        response_list = list(pool.map(single_get, urls))
    return response_list


def single_get_tag(input: list) -> (requests.Response, str):
    return requests.get(input[0], allow_redirects=True), input[1]


def multi_get_tag(urls: list) -> list:
    with concurrent.futures.ThreadPoolExecutor(max_workers=10) as pool:
        response_list = list(pool.map(single_get_tag, urls))
    return response_list


def mrg(url: str) -> bool:
    requests.packages.urllib3.disable_warnings()
    resp = requests.get(url, verify=False)
    soup = bs4.BeautifulSoup(resp.text, "lxml")
    search = soup.find_all("div", class_="label")

    Up_Time = str()
    for elem in search:
        if elem.string == "Last Broadcast:":
            Up_Time = elem.next_sibling.next_sibling.string
            # print(elem.next_sibling.next_sibling)

    if Up_Time.find("minutes") != -1:
        return True
    else:
        return False


def run_cb_scrape() -> None:
    url = json.load(open(os.path_dirname(path) + "/cb.json"))
    if mrg(url["1"]):
        print("mg ", end="")
        vocalize(os.path.expanduser("~") + "/scripts/mila/mgup.ogg")
    if mrg(url["2"]):
        print("obk ", end="")
        vocalize(os.path.expanduser("~") + "/scripts/mila/obkup.ogg")
    if mrg(url["5"]):
        print("ls ", end="")
        vocalize(os.path.expanduser("~") + "/scripts/mila/lisaup.ogg")


def manga_scrape() -> None:
    urls = json.load(open(os.path.dirname(path) + "/manga.json"))
    requests.packages.urllib3.disable_warnings()
    result = str()
    url_list = list()
    [url_list.append(url) for _, url in urls.items()]
    response_list = multi_get(url_list)

    for resp in response_list:
        soup = bs4.BeautifulSoup(resp.text, "lxml")
        search = soup.find_all("a", class_="chapter-name text-nowrap")
        re_res = []
        for thing in search:
            re_res.append(
                green + thing["title"] + " > " + blue + thing["href"]
            )
        try:
            result += re_res[0] + "\n"
        except IndexError:
            result += thing["title"] + "--> nothing\n"
    print(result, end="")


def anime_scrape() -> None:
    urls = json.load(open(os.path.dirname(path) + "/anime.json"))
    requests.packages.urllib3.disable_warnings()
    results = list()
    url_list = list()
    [url_list.append([url, tag]) for tag, url in urls.items()]
    response_list = multi_get_tag(url_list)

    for resp, tag in response_list:
        soup = bs4.BeautifulSoup(resp.text, "lxml")
        search = soup.find_all("a")
        re_res = []

        for thing in search:
            child = thing.findChild("span", class_="jtitle")
            if not child:
                continue
            re_res.append(re.findall("Episode [0-9]*$", thing.text))
        results.append(green + tag + " > " + blue + repr(max(re_res)))
    for result in results:
        print(result)


def vocalize(sound) -> None:
    # import subprocess
    # subprocess.call([os.path.expanduser("~") + "/scripts/voice.sh", sound])
    pass


def main():
    argparser = Argparser()
    if argparser.args.cb:
        run_cb_scrape()
    elif argparser.args.manga:
        manga_scrape()
    elif argparser.args.anime:
        anime_scrape()
    else:
        pass


if __name__ == "__main__":
    main()