next_read.wsgi 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. """
  80. now = datetime.datetime.now()
  81. showed_read = get_dict["showed_read"]
  82. for item in showed_read:
  83. #добавляем показанные вакансии в лог
  84. #INSERT INTO `sf_log` (`user_id`, `date`, `hour`, `action`, `data_1`, `data_2`, `data_3`, `data_4`, `data`, `weight`) VALUES ('', '', '', '', '', '', '', '', '', '');
  85. exist_vacancies_query = "SELECT `id` FROM `sf_log` WHERE (`code` = 'read') AND (`data_1` = " + str(item["id"]) + ") AND (`action` = 'show_next') AND (`user_id` = " + str(wp_id) + ") ;"
  86. with mysql_connection.cursor(buffered=True) as cursor:
  87. cursor.execute(exist_vacancies_query)
  88. exist_vacancies = cursor.fetchall()
  89. out_s["exist_vacancies_query"] = exist_vacancies_query
  90. if (len(exist_vacancies) == 0):
  91. 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_next', '" + str(item["id"]) + "', '', '" + str(item["title"]) + "', '', 'data_1=>read_id, data_3=>read_title', '');"
  92. with mysql_connection.cursor() as cursor:
  93. cursor.execute(mysql_query)
  94. mysql_connection.commit()
  95. """
  96. mysql_query = "UPDATE `sf_log` SET `action` = 'show_next' WHERE `user_id` = '" + str(wp_id) + "' AND `code` = 'read';"
  97. with mysql_connection.cursor() as cursor:
  98. cursor.execute(mysql_query)
  99. mysql_connection.commit()
  100. start_response('200 OK', [('Content-Type','text/html')])
  101. out_s = json.dumps(out_s)
  102. b = out_s.encode('utf-8')
  103. return [b]