-
Notifications
You must be signed in to change notification settings - Fork 17
/
handler.py
78 lines (71 loc) · 2.62 KB
/
handler.py
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
"""Manage Handlers and map subdomains and domain names to Handlers."""
from collections import defaultdict
import tldextract
from handlers.article_handler import ArticleHandler
from handlers.business_times_handler import BusinesstimesHandler
from handlers.cna_lifestyle_handler import CNAlifestyleHandler
from handlers.ricemedia_handler import RicemediaHandler
from handlers.straits_times_handler import STHandler
from handlers.tnp_handler import TNPHandler
from handlers.today_online_handler import TodayonlineHandler
from handlers.yahoo_handler import YahooHandler
class HandlerManager:
"""
Manage Handlers and map subdomains and domain names to Handlers.
"""
handlers = {
"businesstimes.com.sg": defaultdict(
lambda: BusinesstimesHandler
),
"channelnewsasia.com": defaultdict(
lambda: ArticleHandler, # Default Handler for all subdomains (unless specify)
# for channelnewsasia.com
cnalifestyle=CNAlifestyleHandler
),
"mothership.sg": defaultdict(
lambda: ArticleHandler
),
"ricemedia.co": defaultdict(
lambda: RicemediaHandler
),
"straitstimes.com": defaultdict(
lambda: STHandler
),
"tnp.sg": defaultdict(
lambda: TNPHandler
),
"todayonline.com": defaultdict(
lambda: TodayonlineHandler
),
"yahoo.com": defaultdict(
lambda: YahooHandler
),
"zula.sg": defaultdict(
lambda: ArticleHandler
)
}
@classmethod
def extract_hostname(cls, url):
"""Extract subdomain and domain name from URL."""
url_extract = tldextract.extract(url)
domains = {
"subdomain": url_extract.subdomain,
"domain_name": url_extract.domain + "." + url_extract.suffix
}
return domains
@classmethod
def has_handler(cls, url):
"""Check if a submission URL has an appropriate Handler."""
domains = cls.extract_hostname(url)
return domains["domain_name"] in cls.handlers
@classmethod
def get_handler(cls, url):
"""
Return the Handler corresponding to a subdomain and domain name.
Will return the default domain Handler if subdomain Handler do not exist.
Raise ValueError if no domain Handler exists.
"""
if not cls.has_handler(url):
raise ValueError("Domain name {} has no Handler".format(url))
domains = cls.extract_hostname(url)
return cls.handlers[domains["domain_name"]][domains["subdomain"]]