frszvb commited on
Commit
c622c73
·
1 Parent(s): 0fbc4d8

Fix relative CSS URLs with urljoin

Browse files
Files changed (1) hide show
  1. app.py +9 -2
app.py CHANGED
@@ -5,6 +5,7 @@ import re
5
  import tempfile
6
  import zipfile
7
  import requests
 
8
  import gradio as gr
9
  import spaces
10
  from fontTools.ttLib import TTFont
@@ -37,8 +38,14 @@ def extract_from_page(url):
37
  html = r.text
38
  base = r.url
39
  css_links = re.findall(r'href=["\']([^"\']+\.css[^"\']*)["\']', html, re.I)
40
- css_texts = [html] + [cr.text for link in css_links
41
- for cr in [requests.get(("https:" + link) if link.startswith("//") else (base.rstrip("/") + link if link.startswith("/") else link), headers=UA, timeout=20)] if cr.ok]
 
 
 
 
 
 
42
  for css in css_texts:
43
  found += parse_font_faces(css)
44
  found += [(u.split("/")[-1], u, "") for u in re.findall(r'(https?://[^"\']+?\.(?:woff2|woff|ttf|otf|eot))', html, re.I)]
 
5
  import tempfile
6
  import zipfile
7
  import requests
8
+ from urllib.parse import urljoin
9
  import gradio as gr
10
  import spaces
11
  from fontTools.ttLib import TTFont
 
38
  html = r.text
39
  base = r.url
40
  css_links = re.findall(r'href=["\']([^"\']+\.css[^"\']*)["\']', html, re.I)
41
+ css_texts = [html]
42
+ for link in css_links:
43
+ try:
44
+ cr = requests.get(urljoin(base, link), headers=UA, timeout=20)
45
+ if cr.ok:
46
+ css_texts.append(cr.text)
47
+ except Exception:
48
+ pass
49
  for css in css_texts:
50
  found += parse_font_faces(css)
51
  found += [(u.split("/")[-1], u, "") for u in re.findall(r'(https?://[^"\']+?\.(?:woff2|woff|ttf|otf|eot))', html, re.I)]