main_setting_sitemap.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from .tool.func import *
  2. async def main_setting_sitemap(do_type = 0):
  3. with get_db_connect() as conn:
  4. curs = conn.cursor()
  5. if not do_type == 1:
  6. if await acl_check('', 'owner_auth', '', '') == 1:
  7. return await re_error(conn, 0)
  8. if do_type == 1 or flask.request.method == 'POST':
  9. if not do_type == 1:
  10. await acl_check(tool = 'owner_auth', memo = 'make sitemap')
  11. data = '' + \
  12. '<?xml version="1.0" encoding="UTF-8"?>\n' + \
  13. '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n' + \
  14. ''
  15. curs.execute(db_change('select data from other where name = "sitemap_auto_exclude_domain"'))
  16. db_data = curs.fetchall()
  17. if db_data and db_data[0][0] != '':
  18. domain = ''
  19. else:
  20. domain = load_domain(conn, 'full')
  21. sql_add = ''
  22. curs.execute(db_change('select data from other where name = "sitemap_auto_exclude_user_page"'))
  23. db_data = curs.fetchall()
  24. if db_data and db_data[0][0] != '':
  25. sql_add += ' title not like "user:%"'
  26. curs.execute(db_change('select data from other where name = "sitemap_auto_exclude_file_page"'))
  27. db_data = curs.fetchall()
  28. if db_data and db_data[0][0] != '':
  29. if sql_add != '':
  30. sql_add += ' and'
  31. sql_add += ' title not like "file:%"'
  32. curs.execute(db_change('select data from other where name = "sitemap_auto_exclude_category_page"'))
  33. db_data = curs.fetchall()
  34. if db_data and db_data[0][0] != '':
  35. if sql_add != '':
  36. sql_add += ' and'
  37. sql_add += ' title not like "category:%"'
  38. if sql_add != '':
  39. sql_add = ' where' + sql_add
  40. curs.execute(db_change("select title from data" + sql_add))
  41. all_data = curs.fetchall()
  42. len_all_data = len(all_data)
  43. count = int(len_all_data / 30000)
  44. other_count = len_all_data % 30000
  45. for i in range(count + 1):
  46. data += '<sitemap><loc>' + domain + '/sitemap_' + str(i) + '.xml</loc></sitemap>\n'
  47. data += '' + \
  48. '</sitemapindex>' + \
  49. ''
  50. f = open("sitemap.xml", 'w')
  51. f.write(data)
  52. f.close()
  53. for i in range(count + 1):
  54. data = '' + \
  55. '<?xml version="1.0" encoding="UTF-8"?>\n' + \
  56. '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n' + \
  57. ''
  58. if count == i:
  59. for x in all_data[30000 * i:]:
  60. data += '<url><loc>' + domain + '/w/' + url_pas(x[0]) + '</loc></url>\n'
  61. else:
  62. for x in all_data[30000 * i:30000 * (i + 1)]:
  63. data += '<url><loc>' + domain + '/w/' + url_pas(x[0]) + '</loc></url>\n'
  64. data += '' + \
  65. '</urlset>' + \
  66. ''
  67. f = open("sitemap_" + str(i) + ".xml", 'w')
  68. f.write(data)
  69. f.close()
  70. if not do_type == 1:
  71. return redirect(conn, '/setting/sitemap')
  72. else:
  73. return ''
  74. else:
  75. return easy_minify(conn, flask.render_template(skin_check(conn),
  76. imp = [get_lang(conn, 'sitemap_manual_create'), wiki_set(conn), await wiki_custom(conn), wiki_css([0, 0])],
  77. data = '''
  78. <form method="post">
  79. <button id="opennamu_save_button" type="submit">''' + get_lang(conn, 'create') + '''</button>
  80. </form>
  81. ''',
  82. menu = [['setting/sitemap_set', get_lang(conn, 'return')]]
  83. ))