This repository has been archived by the owner on Nov 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker_sample.py
78 lines (57 loc) · 2.26 KB
/
checker_sample.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
import asyncio
import json
import logging
import cryptography.fernet as fernet
import pyppeteer
from adchecklib import Down
mainLogger = logging.getLogger(__name__)
SECRET_KEY = b"gno8p-srs5tko8rZRN3a7EtPhd0WObZqJlmeO-eq-kU="
async def check_advertisement(page, logger):
integrity = await page.evaluate('''() => {
return window.adhellIntegrity;
}''')
if not isinstance(integrity, str):
raise Down("Please disable AdBlocker (no integrity)")
cipher = fernet.Fernet(SECRET_KEY)
try:
data = cipher.decrypt(integrity.encode(), 60).decode()
banner = json.loads(data)
except (fernet.InvalidToken, UnicodeDecodeError, json.JSONDecodeError) as exc:
raise Down(f"Please disable AdBlocker ({type(exc)})")
logger.info(data)
tag_name = {
'text': 'p',
'image': 'img',
'video': 'video'
}[banner['format']]
logger.info("Banner contents: %s", await page.evaluate('''() => {
return document.querySelector('#adhellRealBanner').outerHTML;
}'''))
has_block = await page.evaluate(f'''() => {{
const bl = document.querySelector("#adhellRealBanner #{banner['block']}");
return bl && bl.tagName.toUpperCase() == '{tag_name.upper()}'
}}''')
if not has_block:
raise Down("Please disable AdBlocker (no content block)")
if banner['partner']:
if banner['format'] == 'text':
has_content = await page.evaluate(f'''() => {{
const bl = document.querySelector("#adhellRealBanner #{banner['block']}");
return bl && bl.innerHTML == "{banner['content']}";
}}''')
else:
has_content = await page.evaluate(f'''() => {{
const bl = document.querySelector("#adhellRealBanner #{banner['block']}");
return bl && bl.src == "{banner['content']}";
}}''')
if not has_content:
raise Down("Please disable AdBlocker (corrupted content)")
async def main():
browser = await pyppeteer.launch()
page = await browser.newPage()
await page.goto('http://127.0.0.1:43250/sample.html')
try:
await check_advertisement(page, mainLogger)
finally:
await browser.close()
asyncio.get_event_loop().run_until_complete(main())