aboutsummaryrefslogtreecommitdiff
path: root/episode.py
blob: 6ac8ab191ba0e7ee15eec13247be6a8292901d78 (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
#!/usr/bin/env python3

class EpisodeInfo:
    group_name = None
    anime_name = None
    episode = None
    resolution = None
    extension = None

    def get_generic_name(self):
        if not self.group_name or not self.anime_name:
            return ""
        generic_name =  "[%s] %s" % (self.group_name, self.anime_name)
        if self.resolution:
            generic_name += " [%s]" % self.resolution
        if self.extension:
            generic_name += self.extension
        return generic_name

def episode_extract_info(episode_name):
    episode_name = episode_name.strip()
    episode_info = EpisodeInfo()

    if len(episode_name) == 0:
        return episode_info

    extension_index = episode_name.rfind('.')
    if extension_index != -1:
        episode_info.extension = episode_name[extension_index:]
        episode_name = episode_name[0:extension_index]

    if episode_name[0] != '[':
        return episode_info

    group_name_end = episode_name.find(']')
    if group_name_end == -1:
        return episode_info

    episode_info.group_name = episode_name[1:group_name_end].strip()

    last_dash = episode_name.rfind('-')
    if last_dash == -1:
        return episode_info

    episode_info.anime_name = episode_name[group_name_end+1:last_dash].strip()

    resolution_index = episode_name.find('[', last_dash + 1)
    if resolution_index == -1:
        episode_info.episode = episode_name[last_dash+1:].strip()
    else:
        episode_info.episode = episode_name[last_dash+1:resolution_index].strip()
        resolution_end = episode_name.find(']', resolution_index + 1)
        if resolution_end != -1:
            episode_info.resolution = episode_name[resolution_index+1:resolution_end]

    return episode_info

if __name__ == "__main__":
    episode_info = episode_extract_info("[Erai-raws] Saiki Kusuo no Psi Nan - Kanketsu-hen - 01 [1080p][Multiple Subtitle].mkv")
    print("|{}|".format(episode_info.group_name))
    print("|{}|".format(episode_info.anime_name))
    print("|{}|".format(episode_info.episode))
    print("|{}|".format(episode_info.resolution))
    print("|{}|".format(episode_info.extension))
    print("|{}|".format(episode_info.get_generic_name()))

    episode_info = episode_extract_info("nyaa.si")
    print("|{}|".format(episode_info.group_name))
    print("|{}|".format(episode_info.anime_name))
    print("|{}|".format(episode_info.episode))
    print("|{}|".format(episode_info.resolution))
    print("|{}|".format(episode_info.extension))
    print("|{}|".format(episode_info.get_generic_name()))