update_index.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import requests
  2. from datetime import datetime
  3. import xml.etree.ElementTree as ET
  4. from xml.dom import minidom
  5. import pytz
  6. import os
  7. def update_sitemap():
  8. # 获取项目根目录的 sitemap.xml 路径
  9. root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  10. sitemap_path = os.path.join(root_dir, 'sitemap.xml')
  11. # 读取现有的 sitemap.xml
  12. tree = ET.parse(sitemap_path)
  13. root = tree.getroot()
  14. # 获取中国时区的当前日期
  15. tz = pytz.timezone('Asia/Shanghai')
  16. current_date = datetime.now(tz).strftime('%Y-%m-%d')
  17. # 更新所有 URL 的 lastmod
  18. for url in root.findall('.//{http://www.sitemaps.org/schemas/sitemap/0.9}url'):
  19. lastmod = url.find('{http://www.sitemaps.org/schemas/sitemap/0.9}lastmod')
  20. if lastmod is not None:
  21. lastmod.text = current_date
  22. # 转换为格式化的 XML 字符串
  23. xmlstr = minidom.parseString(ET.tostring(root)).toprettyxml(indent=' ')
  24. # 移除空行
  25. xmlstr = '\n'.join([s for s in xmlstr.splitlines() if s.strip()])
  26. # 保存更新后的 sitemap
  27. with open(sitemap_path, 'w', encoding='UTF-8') as f:
  28. f.write(xmlstr)
  29. print(f"Sitemap updated with date: {current_date}")
  30. def notify_index_now():
  31. urls = [
  32. "https://live.izbds.com",
  33. "https://live.izbds.com/tv/iptv6.txt",
  34. "https://live.izbds.com/tv/iptv6.m3u",
  35. "https://live.izbds.com/tv/iptv4.txt",
  36. "https://live.izbds.com/tv/iptv4.m3u",
  37. "https://live.izbds.com/tools/"
  38. ]
  39. for site_url in urls:
  40. url = "https://www.bing.com/indexnow"
  41. params = {
  42. "url": site_url,
  43. "key": "4c761c7c12a64659b9529b778f6a3b75"
  44. }
  45. response = requests.get(url, params=params)
  46. if response.status_code == 200:
  47. print(f"Successfully notified IndexNow for {site_url}")
  48. else:
  49. print(f"Failed to notify IndexNow for {site_url}: {response.status_code}")
  50. if __name__ == "__main__":
  51. update_sitemap()
  52. notify_index_now()