forked from matomo-org/matomo-icons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
referrers.py
124 lines (111 loc) · 4.77 KB
/
referrers.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
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
#!/usr/bin/env python3
# Copyright (C) 2017 Lukas Winkler
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
import shutil
import sys
import urllib.parse
import os.path
import requests
import yaml
from bs4 import BeautifulSoup
def load_yaml(file):
with open(file, 'r') as stream:
return yaml.load(stream)
def download_favicon(homepage_html, url, target_file):
"""
Detect favicon if linked via "<link rel="shortcut icon" href="/favicon.ico">"
otherwise assume /favicon.ico
"""
print(url, target_file)
soup = BeautifulSoup(homepage_html, "html.parser")
favicon_element = soup.find("link", rel="shortcut icon")
if favicon_element and favicon_element.has_attr("href"):
favicon_path = favicon_element['href']
elif soup.find("link", rel="icon") and soup.find("link", rel="icon").has_attr("href"):
# some sites don't use "shortcut icon" for favicon
# in this case we take the first other icon and hope it fits
favicon_path = soup.find("link", rel="icon")["href"]
else:
favicon_path = "/favicon.ico"
print(favicon_path)
# Works with relative and absolute favicon_paths:
favicon_url = urllib.parse.urljoin("http://" + url, favicon_path)
print(favicon_url)
try:
r = requests.get(favicon_url, stream=True)
if r.status_code == 200:
with open(outputdir + target_file + ".ico", 'wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
return True
except (
requests.exceptions.ConnectionError,
requests.exceptions.InvalidSchema,
requests.exceptions.TooManyRedirects):
print("Error while downloading favicon")
def main(search_engines):
for i, element in search_engines.items():
if MODE == "searchengines":
search_engine = element[0]
urls = search_engine["urls"]
else:
urls = element
first_url = None
success = False
for url in urls:
if "{}" not in url and "/" not in url:
if first_url is None:
first_url = url
print(url)
if not os.path.isfile(outputdir + first_url + ".ico"):
try:
offline = False
r = requests.get("http://" + url, timeout=15)
r.raise_for_status()
except requests.exceptions.ReadTimeout as e:
print("http://" + url + " " + "Timeout", file=sys.stderr)
offline = True
except requests.exceptions.TooManyRedirects as e:
print("http://" + url + " " + "Too many Redirects", file=sys.stderr)
offline = True
except (requests.exceptions.HTTPError, requests.exceptions.ConnectionError,
requests.exceptions.TooManyRedirects) as e:
print("http://" + url + " " + str(e), file=sys.stderr)
offline = True
except requests.exceptions.RequestException as e:
print("http://" + url + " " + str(e.args[0].reason), file=sys.stderr)
offline = True
if not offline:
success = download_favicon(r.content, url, first_url)
if success:
break
else:
print("file already downloaded")
if __name__ == "__main__":
MODE = sys.argv[1] if len(sys.argv) >= 2 else ""
if MODE == "searchengines":
yamlfile = "vendor/piwik/searchengine-and-social-list/SearchEngines.yml"
outputdir = "src/Referrers/images/searchEngines/"
elif MODE == "socials":
yamlfile = "vendor/piwik/searchengine-and-social-list/Socials.yml"
outputdir = "src/Referrers/images/socials/"
else:
yamlfile = outputdir = False
print('Invalid mode. Valid modes: "searchengines" or "socials"')
exit(1)
try:
finished = [line.rstrip('\n') for line in open('finished.txt')]
except FileNotFoundError:
finished = []
main(load_yaml(yamlfile))