best_read.wsgi 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # -*- encoding: utf-8 -*-
  2. #
  3. import os, json, sys, subprocess, urllib.parse, traceback
  4. import random, time, datetime, re
  5. from urllib.parse import unquote
  6. virtual_env = os.path.expanduser('~/projects/world-it-planet/env')
  7. activate_this = os.path.join(virtual_env, 'bin/activate_this.py')
  8. exec(open(activate_this).read(), dict(__file__=activate_this))
  9. import requests
  10. #MySql
  11. from mysql.connector import connect, Error
  12. #декларативное определение SQLLite
  13. from sqlalchemy import Column, Integer, String, Text, create_engine
  14. from sqlalchemy.ext.declarative import declarative_base
  15. from sqlalchemy.orm import sessionmaker
  16. from sqlalchemy import desc
  17. #---------------------------------- Variables ----------
  18. #---------------------------------- Variables End ----------
  19. def application(env, start_response):
  20. out_s = {}
  21. """
  22. for key in env:
  23. out_s = out_s + str(key) + "=" + str(env[key]) + "<br>"
  24. """
  25. #получаем $_GET из запроса
  26. get_query = env['QUERY_STRING']
  27. get_json = get_query.replace("q=", "")
  28. get_json = unquote(get_json)
  29. get_dict = json.loads(get_json)
  30. tm_id = str(get_dict['tm_id'])
  31. vk_id = str(get_dict['vk_id'])
  32. out_s["tm_id"] = tm_id
  33. #Инициализация MySQL
  34. mysql_connection = connect(
  35. host="localhost",
  36. user="id35114350",
  37. password="Hgatrdy5rTeq",
  38. database="id35114350_steelfeet",
  39. charset='utf8',
  40. use_unicode=True
  41. )
  42. #Инициализация SQLLite
  43. basedir = os.path.abspath(os.path.dirname(__file__))
  44. SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'habr.db')
  45. engine = create_engine(SQLALCHEMY_DATABASE_URI, pool_pre_ping=True)
  46. Base = declarative_base()
  47. class Links(Base):
  48. __tablename__ = 'links'
  49. id = Column(Integer, primary_key=True, autoincrement=True)
  50. title = Column(String(512))
  51. href = Column(String(512))
  52. donor = Column(String(255))
  53. donor_link_id = Column(Integer) #внутренний идентификатор для донора, https://habr.com/ru/company/skillfactory/blog/578014/ -> 578014
  54. parse_date = Column(Integer)
  55. text = Column(Text)
  56. def __init__(self, title, href, donor, donor_link_id, parse_date, text):
  57. self.title = title
  58. self.href = href
  59. self.donor = donor
  60. self.donor_link_id = donor_link_id
  61. self.parse_date = parse_date
  62. self.text = text
  63. def __repr__(self):
  64. return "<Link('%s', '%s')>" % (self.title, self.href)
  65. Session = sessionmaker(bind=engine)
  66. sqllite_session = Session()
  67. #------------------------------------------ Основной цикл ------------------
  68. #запрос wp_id по внешнему сервису
  69. params = {
  70. "action":"show_wp_id",
  71. "tm_id":tm_id
  72. }
  73. params_json = json.dumps(params)
  74. get_wp_url = "https://steelfeet.ru/app/get.php?q=" + params_json
  75. out_s["get_wp_url"] = get_wp_url
  76. response = requests.get(get_wp_url)
  77. wp_id = int(response.text)
  78. out_s["wp_id"] = wp_id
  79. now = datetime.datetime.now()
  80. #links = sqllite_session.query(Links).order_by(desc(Links.parse_date))[0:5]
  81. #отбираем показанные вакансии (скорее, помеченные как прочитанные)
  82. showed_read_query = "SELECT `data_1`, `data_3`, `weight` FROM `sf_log` WHERE (`code` = 'read') AND (`action` = 'show_next') AND (`user_id` = " + str(wp_id) + ");"
  83. #out_s["showed_vacancies_query"] = showed_vacancies_query
  84. with mysql_connection.cursor(buffered=True) as cursor:
  85. cursor.execute(showed_read_query)
  86. showed_read = cursor.fetchall()
  87. showed_read_ids = []
  88. for item in showed_read:
  89. item_id, item_data_3, item_weight = item
  90. showed_read_ids.append(item_id)
  91. out_s["showed_read_ids"] = showed_read_ids
  92. #считаем статистику слов
  93. #отбираем все уже показанные ссылки
  94. all_read_query = "SELECT `data_1`, `data_3`, `weight` FROM `sf_log` WHERE (`code` = 'read') AND (`user_id` = " + str(wp_id) + ");"
  95. with mysql_connection.cursor(buffered=True) as cursor:
  96. cursor.execute(all_read_query)
  97. all_read = cursor.fetchall()
  98. words_stat = {}
  99. for item in all_read:
  100. item_id, item_data_3, item_weight = item
  101. words = str(item_data_3).replace('-',' ').replace('/',' ').replace('\\',' ').replace('(','').replace(')','').split(" ")
  102. for word in words:
  103. try:
  104. if (len(word) > 0):
  105. words_stat[word] = words_stat[word] + item_weight
  106. except:
  107. words_stat[word] = item_weight
  108. out_s["words"] = words_stat
  109. #считаем веса для непоказанных вакансий
  110. reads = sqllite_session.query(Links).order_by(desc(Links.parse_date))[0:500]
  111. reads_list = []
  112. for item in reads:
  113. #непоказанные
  114. if (not(item.id in showed_read_ids)):
  115. words = str(item.title).replace('-',' ').replace('/',' ').replace('\\',' ').replace('(','').replace(')','').split(" ")
  116. read_weight = 0
  117. for word in words:
  118. try:
  119. if (len(word) > 0):
  120. read_weight = read_weight + words_stat[word]
  121. except:
  122. pass
  123. read_item = {
  124. "id" : item.id,
  125. "weight" : read_weight,
  126. "title" : str(item.title),
  127. "href" : item.href,
  128. }
  129. reads_list.append(read_item)
  130. #сортируем по весу
  131. reads_list = sorted(reads_list, key=lambda x: x["weight"], reverse=True)
  132. #выводим лучшие 5
  133. reads_list = reads_list[0:5]
  134. for item in reads_list:
  135. #добавляем показанные вакансии в лог
  136. #INSERT INTO `sf_log` (`user_id`, `date`, `hour`, `action`, `data_1`, `data_2`, `data_3`, `data_4`, `data`, `weight`) VALUES ('', '', '', '', '', '', '', '', '', '');
  137. exist_vacancies_query = "SELECT `id` FROM `sf_log` WHERE (`code` = 'read') AND (`data_1` = " + str(item["id"]) + ");"
  138. with mysql_connection.cursor(buffered=True) as cursor:
  139. cursor.execute(exist_vacancies_query)
  140. exist_vacancies = cursor.fetchall()
  141. mysql_query = ""
  142. if (len(exist_vacancies) == 0):
  143. mysql_query = "INSERT INTO `sf_log` (`user_id`, `date`, `hour`, `code`, `action`, `data_1`, `data_2`, `data_3`, `data_4`, `data`, `weight`) VALUES ('" + str(wp_id) + "', '" + str(int(time.time())) + "', '" + str(now.hour) + "', 'read', 'show_best', '" + str(item["id"]) + "', '', '" + str(item["title"]) + "', '', 'data_1=>read_id, data_3=>read_title', 0);"
  144. with mysql_connection.cursor() as cursor:
  145. cursor.execute(mysql_query)
  146. mysql_connection.commit()
  147. out_s["reads"] = reads_list
  148. start_response('200 OK', [('Content-Type','text/html')])
  149. out_s = json.dumps(out_s)
  150. b = out_s.encode('utf-8')
  151. return [b]