next_vacancies.wsgi 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. response = requests.get(get_wp_url)
  89. wp_id = int(response.text)
  90. out_s["wp_id"] = wp_id
  91. """
  92. now = datetime.datetime.now()
  93. showed_vacancies = get_dict["showed_vacancies"]
  94. for item in showed_vacancies:
  95. #добавляем показанные вакансии в лог
  96. #INSERT INTO `sf_log` (`user_id`, `date`, `hour`, `action`, `data_1`, `data_2`, `data_3`, `data_4`, `data`, `weight`) VALUES ('', '', '', '', '', '', '', '', '', '');
  97. exist_vacancies_query = "SELECT `id` FROM `sf_log` WHERE (`code` = 'vacancy') AND (`data_1` = " + str(item["id"]) + ");"
  98. with mysql_connection.cursor(buffered=True) as cursor:
  99. cursor.execute(exist_vacancies_query)
  100. exist_vacancies = cursor.fetchall()
  101. if (len(exist_vacancies) == 0):
  102. 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_next', '" + str(item["id"]) + "', '', '" + str(item["title"]) + "', '', 'data_1=>vacancy_id, data_3=>vacancy_title', '');"
  103. with mysql_connection.cursor() as cursor:
  104. cursor.execute(mysql_query)
  105. mysql_connection.commit()
  106. """
  107. mysql_query = "UPDATE `sf_log` SET `action` = 'show_next' WHERE `user_id` = '" + str(wp_id) + "' AND `code` = 'vacancy';"
  108. with mysql_connection.cursor() as cursor:
  109. cursor.execute(mysql_query)
  110. mysql_connection.commit()
  111. start_response('200 OK', [('Content-Type','text/html')])
  112. out_s = json.dumps(out_s)
  113. b = out_s.encode('utf-8')
  114. return [b]