update_deps.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import os
  2. import requests
  3. from bs4 import BeautifulSoup
  4. from urllib.parse import urljoin, urlparse
  5. import re
  6. def download_file(url, local_path):
  7. response = requests.get(url)
  8. if response.status_code == 200:
  9. os.makedirs(os.path.dirname(local_path), exist_ok=True)
  10. with open(local_path, 'wb') as f:
  11. f.write(response.content)
  12. print(f"Downloaded: {local_path}")
  13. else:
  14. print(response.status_code)
  15. print(f"Failed to download: {url}")
  16. def update_html(html_content, base_url):
  17. soup = BeautifulSoup(html_content, 'html.parser')
  18. for tag in soup.find_all(['script', 'link']):
  19. if tag.has_attr('src'):
  20. url = tag['src']
  21. elif tag.has_attr('href'):
  22. url = tag['href']
  23. else:
  24. continue
  25. if url.startswith(('http://', 'https://')):
  26. full_url = url
  27. else:
  28. full_url = urljoin(base_url, url)
  29. parsed_url = urlparse(full_url)
  30. local_path = os.path.join('static', parsed_url.netloc, parsed_url.path.lstrip('/'))
  31. download_file(full_url, local_path)
  32. relative_path = os.path.relpath(local_path, '.')
  33. if tag.name == 'script':
  34. tag['src'] = "/" + relative_path
  35. elif tag.name == 'link':
  36. tag['href'] = "/" + relative_path
  37. return str(soup)
  38. # Read the HTML file
  39. with open('./index.html', 'r') as f:
  40. html_content = f.read()
  41. # Update HTML and download files
  42. # updated_html = update_html(html_content, 'https://example.com')
  43. # # Write the updated HTML
  44. # with open('./index.html', 'w') as f:
  45. # f.write(updated_html)
  46. print("HTML file updated with local paths.")
  47. # Download Font Awesome CSS and font files
  48. base_url = "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/"
  49. css_url = urljoin(base_url, "css/all.min.css")
  50. output_dir = "static/cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2"
  51. # Download CSS file
  52. css_output_path = os.path.join(output_dir, "css", "all.min.css")
  53. download_file(css_url, css_output_path)
  54. # Parse CSS file for font URLs
  55. with open(css_output_path, 'r', encoding='utf-8') as f:
  56. css_content = f.read()
  57. # Extract font URLs from the CSS content
  58. font_urls = re.findall(r'url\((.*?\.(?:woff2|ttf))\)', css_content)
  59. print(f"Found {len(font_urls)} font URLs")
  60. # Download font files
  61. for font_url in font_urls:
  62. font_url = font_url.strip('"\'')
  63. if font_url.startswith('../'):
  64. font_url = font_url[3:]
  65. # Use base_url instead of urljoin to keep the version number
  66. full_url = base_url + font_url
  67. relative_path = font_url
  68. output_path = os.path.join(output_dir, relative_path)
  69. download_file(full_url, output_path)
  70. print("Download complete!")