best_vacancies.wsgi 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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, 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. out_s["tm_id"] = tm_id
  32. #Инициализация MySQL
  33. mysql_connection = connect(
  34. host="localhost",
  35. user="id35114350",
  36. password="Hgatrdy5rTeq",
  37. database="id35114350_steelfeet",
  38. charset='utf8',
  39. use_unicode=True
  40. )
  41. #Инициализация SQLLite
  42. basedir = os.path.abspath(os.path.dirname(__file__))
  43. SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'hhtm.db')
  44. engine = create_engine(SQLALCHEMY_DATABASE_URI, pool_pre_ping=True)
  45. Base = declarative_base()
  46. class Vacancies(Base):
  47. __tablename__ = 'vacancies'
  48. id = Column(Integer, primary_key=True, autoincrement=True)
  49. title = Column(String(512))
  50. city = Column(String(20))
  51. specialization = Column(String(255))
  52. href = Column(String(512))
  53. donor = Column(String(255))
  54. vacancy_id = Column(Integer)
  55. vacancy_date = Column(Integer)
  56. parse_date = Column(Integer)
  57. employer = Column(String(255))
  58. canal_city_id = Column(Integer)
  59. canal_city_date = Column(Integer)
  60. canal_spec_id = Column(Integer)
  61. canal_spec_date = Column(Integer)
  62. def __init__(self, title, city, specialization, href, donor, vacancy_id, vacancy_date, parse_date, employer, canal_city_id, canal_city_date, canal_spec_id, canal_spec_date):
  63. self.title = title
  64. self.city = city
  65. self.specialization = specialization
  66. self.href = href
  67. self.donor = donor
  68. self.vacancy_id = vacancy_id
  69. self.vacancy_date = vacancy_date
  70. self.parse_date = parse_date
  71. self.employer = employer
  72. self.canal_city_id = canal_city_id
  73. self.canal_city_date = canal_city_date
  74. self.canal_spec_id = canal_spec_id
  75. self.canal_spec_date = canal_spec_date
  76. def __repr__(self):
  77. return "<Vacancy('%s','%s', '%s')>" % (self.title, self.specialization, self.href)
  78. Session = sessionmaker(bind=engine)
  79. sqllite_session = Session()
  80. #------------------------------------------ Основной цикл ------------------
  81. #запрос wp_id по внешнему сервису
  82. params = {
  83. "action":"show_wp_id",
  84. "tm_id":tm_id
  85. }
  86. params_json = json.dumps(params)
  87. get_wp_url = "https://steelfeet.ru/app/get.php?q=" + params_json
  88. out_s["get_wp_url"] = get_wp_url
  89. response = requests.get(get_wp_url)
  90. wp_id = int(response.text)
  91. out_s["wp_id"] = wp_id
  92. now = datetime.datetime.now()
  93. #просто последние пять спарсенных
  94. #vacancies = sqllite_session.query(Vacancies).order_by(desc(Vacancies.parse_date))[0:5]
  95. #отбираем показанные вакансии
  96. showed_vacancies_query = "SELECT `data_1`, `data_3`, `weight` FROM `sf_log` WHERE (`code` = 'vacancy') AND (`action` = 'show_next') AND (`user_id` = " + str(wp_id) + ");"
  97. #out_s["showed_vacancies_query"] = showed_vacancies_query
  98. with mysql_connection.cursor(buffered=True) as cursor:
  99. cursor.execute(showed_vacancies_query)
  100. showed_vacancies = cursor.fetchall()
  101. showed_vacancies_ids = []
  102. for item in showed_vacancies:
  103. item_id, item_data_3, item_weight = item
  104. showed_vacancies_ids.append(item_id)
  105. #считаем статистику слов
  106. #отбираем все вакансии
  107. all_vacancies_query = "SELECT `data_1`, `data_3`, `weight` FROM `sf_log` WHERE (`code` = 'vacancy') AND (`user_id` = " + str(wp_id) + ");"
  108. with mysql_connection.cursor(buffered=True) as cursor:
  109. cursor.execute(all_vacancies_query)
  110. all_vacancies = cursor.fetchall()
  111. words_stat = {}
  112. for item in all_vacancies:
  113. item_id, item_data_3, item_weight = item
  114. words = str(item_data_3).replace('-',' ').replace('/',' ').replace('\\',' ').replace('(','').replace(')','').split(" ")
  115. for word in words:
  116. try:
  117. if (len(word) > 0):
  118. words_stat[word] = words_stat[word] + item_weight
  119. except:
  120. words_stat[word] = item_weight
  121. out_s["words"] = words_stat
  122. #считаем веса для непоказанных вакансий
  123. vacancies = sqllite_session.query(Vacancies).order_by(desc(Vacancies.parse_date))[0:500]
  124. vacancies_list = []
  125. for item in vacancies:
  126. #непоказанные
  127. if (not(item.id in showed_vacancies_ids)):
  128. words = str(item.title).replace('-',' ').replace('/',' ').replace('\\',' ').replace('(','').replace(')','').split(" ")
  129. vacancy_weight = 0
  130. for word in words:
  131. try:
  132. if (len(word) > 0):
  133. vacancy_weight = vacancy_weight + words_stat[word]
  134. except:
  135. pass
  136. vacancy_item = {
  137. "id" : item.id,
  138. "weight" : vacancy_weight,
  139. "title" : str(item.title),
  140. "href" : item.href,
  141. }
  142. vacancies_list.append(vacancy_item)
  143. #сортируем по весу
  144. vacancies_list = sorted(vacancies_list, key=lambda x: x["weight"], reverse=True)
  145. #выводим лучшие 5
  146. vacancies_list = vacancies_list[0:5]
  147. for item in vacancies_list:
  148. #добавляем показанные вакансии в лог
  149. #INSERT INTO `sf_log` (`user_id`, `date`, `hour`, `action`, `data_1`, `data_2`, `data_3`, `data_4`, `data`, `weight`) VALUES ('', '', '', '', '', '', '', '', '', '');
  150. exist_vacancies_query = "SELECT `id` FROM `sf_log` WHERE (`code` = 'vacancy') AND (`data_1` = " + str(item["id"]) + ");"
  151. with mysql_connection.cursor(buffered=True) as cursor:
  152. cursor.execute(exist_vacancies_query)
  153. exist_vacancies = cursor.fetchall()
  154. if (len(exist_vacancies) == 0):
  155. 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) + "', 'vacancy', 'show_best', '" + str(item["id"]) + "', '', '" + str(item["title"]) + "', '', 'data_1=>vacancy_id, data_3=>vacancy_title', '');"
  156. with mysql_connection.cursor() as cursor:
  157. cursor.execute(mysql_query)
  158. mysql_connection.commit()
  159. out_s["vacancies"] = vacancies_list
  160. start_response('200 OK', [('Content-Type','text/html')])
  161. out_s = json.dumps(out_s)
  162. b = out_s.encode('utf-8')
  163. return [b]