-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_db.py
59 lines (48 loc) · 1.55 KB
/
build_db.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
#
# Gathers all Privacy Policy urls that are displayed from the top 100
# free apps on the Google Play store. Uses the 42 matters (paid) API,
# which offers free trials.
#
# Requests to the Play store are separated by 1 second to avoid any
# issues with Google.
#
import os
from urllib import parse
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from pyvirtualdisplay import Display
from termcolor import colored
fin = open('policy_urls.txt')
urls = fin.read().strip().split('\n')
# Slower with headless browser than direct requests,
# but allows for rendering of dynamic content.
display = Display(visible=0, size=(800, 600))
display.start()
browser = webdriver.Firefox()
browser.set_page_load_timeout(10)
# Gets text from html using approach
for url in urls:
host = parse.urlparse(url).hostname
host = host.replace('www.', '')
policy_dir = 'policies'
print("Processing %s" % host)
policy_path = os.path.join(policy_dir, host)
if os.path.exists(policy_path):
# Wooo colored printing
print(colored("Policy already exists", 'yellow'))
continue
try:
browser.get(url)
except TimeoutException:
print("Timeout")
continue
clear_elements = ['header', 'footer']
for ce in clear_elements:
try:
browser.find_element_by_tag_name(ce).clear()
except:
continue
html_page = browser.find_element_by_tag_name('body')
text = html_page.text.replace('\n', '\n\n')
fout = open(policy_path, 'w')
fout.write(text)