api_sitemap.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from .tool.func import *
  2. def api_sitemap():
  3. with get_db_connect() as conn:
  4. curs = conn.cursor()
  5. if admin_check(None, 'make sitemap') == 1:
  6. data = '' + \
  7. '<?xml version="1.0" encoding="UTF-8"?>\n' + \
  8. '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n' + \
  9. ''
  10. domain = load_domain()
  11. curs.execute(db_change("select title from data"))
  12. all_data = curs.fetchall()
  13. len_all_data = len(all_data)
  14. count = int(len_all_data / 30000)
  15. other_count = len_all_data % 30000
  16. for i in range(count + 1):
  17. data += '<sitemap><loc>' + domain + 'sitemap_' + str(i) + '.xml</loc></sitemap>\n'
  18. data += '' + \
  19. '</sitemapindex>' + \
  20. ''
  21. f = open("sitemap.xml", 'w')
  22. f.write(data)
  23. f.close()
  24. e = 0
  25. for i in range(count + 1):
  26. data = '' + \
  27. '<?xml version="1.0" encoding="UTF-8"?>\n' + \
  28. '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n' + \
  29. ''
  30. if count == i:
  31. for x in all_data[30000 * i:]:
  32. data += '<url><loc>' + domain + 'w/' + url_pas(x[0]) + '</loc></url>\n'
  33. else:
  34. for x in all_data[30000 * i:30000 * (i + 1)]:
  35. data += '<url><loc>' + domain + 'w/' + url_pas(x[0]) + '</loc></url>\n'
  36. data += '' + \
  37. '</urlset>' + \
  38. ''
  39. f = open("sitemap_" + str(i) + ".xml", 'w')
  40. f.write(data)
  41. f.close()
  42. return redirect('/sitemap.xml')
  43. else:
  44. return re_error('/ban')