1
0

tm_bot.wsgi 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # -*- encoding: utf-8 -*-
  2. #
  3. import os, json, sys, subprocess, urllib.parse, traceback
  4. virtual_env = os.path.expanduser('~/projects/world-it-planet/env')
  5. activate_this = os.path.join(virtual_env, 'bin/activate_this.py')
  6. exec(open(activate_this).read(), dict(__file__=activate_this))
  7. import random, time, datetime, os, re
  8. import telegram
  9. #декларативное определение
  10. from sqlalchemy import Column, Integer, String, create_engine
  11. from sqlalchemy.ext.declarative import declarative_base
  12. from sqlalchemy.orm import sessionmaker
  13. #---------------------------------- Variables ----------
  14. areas = {"bryansk":19}
  15. specializations = ['программист', 'стажер', 'стажировка']
  16. #максимальное количество публикуемых вакансий за 1 раз
  17. max_vacancies = 3
  18. access_token = "1775925477:AAH1Wlk22hxjSghqOH_IEuolj_FWO2k_YUs"
  19. chat_id = "-1001166215020"
  20. #---------------------------------- Variables End ----------
  21. def application(env, start_response):
  22. out_s = ""
  23. #Инициализация SQLLite
  24. basedir = os.path.abspath(os.path.dirname(__file__))
  25. SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'hhtm.db')
  26. engine = create_engine(SQLALCHEMY_DATABASE_URI, pool_pre_ping=True)
  27. Base = declarative_base()
  28. class Vacancies(Base):
  29. __tablename__ = 'vacancies'
  30. id = Column(Integer, primary_key=True, autoincrement=True)
  31. title = Column(String(512))
  32. city = Column(String(20))
  33. specialization = Column(String(255))
  34. href = Column(String(512))
  35. donor = Column(String(255))
  36. vacancy_id = Column(Integer)
  37. vacancy_date = Column(Integer)
  38. parse_date = Column(Integer)
  39. employer = Column(String(255))
  40. canal_city_id = Column(Integer)
  41. canal_city_date = Column(Integer)
  42. canal_spec_id = Column(Integer)
  43. canal_spec_date = Column(Integer)
  44. 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):
  45. self.title = title
  46. self.city = city
  47. self.specialization = specialization
  48. self.href = href
  49. self.donor = donor
  50. self.vacancy_id = vacancy_id
  51. self.vacancy_date = vacancy_date
  52. self.parse_date = parse_date
  53. self.employer = employer
  54. self.canal_city_id = canal_city_id
  55. self.canal_city_date = canal_city_date
  56. self.canal_spec_id = canal_spec_id
  57. self.canal_spec_date = canal_spec_date
  58. def __repr__(self):
  59. return "<Vacancy('%s','%s', '%s')>" % (self.title, self.specialization, self.href)
  60. class Log(Base):
  61. __tablename__ = 'log'
  62. id = Column(Integer, primary_key=True, autoincrement=True)
  63. action = Column(String(64))
  64. status = Column(String(64))
  65. time = Column(Integer)
  66. donor = Column(String(64))
  67. city = Column(String(20))
  68. specialization = Column(String(20))
  69. vacancies_count = Column(Integer)
  70. canal_id = Column(String(64))
  71. def __init__(self, action, status, time, donor, city, specialization, vacancies_count, canal_id):
  72. self.action = action
  73. self.status = status
  74. self.time = time
  75. self.donor = donor
  76. self.city = city
  77. self.specialization = specialization
  78. self.vacancies_count = vacancies_count
  79. self.canal_id = canal_id
  80. def __repr__(self):
  81. return "<Log('%s','%s', '%s')>" % (self.action, self.status)
  82. Session = sessionmaker(bind=engine)
  83. session = Session()
  84. city, area = random.choice(list(areas.items()))
  85. specialization = random.choice(specializations)
  86. now = datetime.datetime.now()
  87. hour_now = int(now.strftime("%H"))
  88. if ((hour_now > 8) and (hour_now < 20)):
  89. vacancy = session.query(Vacancies).filter(Vacancies.canal_city_date == 0).first()
  90. try:
  91. bot = telegram.Bot(token=access_token)
  92. text = vacancy.title + ": <a href='" + vacancy.href + "'>" + vacancy.href + "</a>"
  93. tm_response = str(bot.sendMessage(chat_id=chat_id, text=text, parse_mode=telegram.ParseMode.HTML))
  94. #ответ не стандартный json
  95. tm_response = tm_response.replace("\\", "\\\\")
  96. tm_response = tm_response.strip("'<>() ").replace("\'", "\"")
  97. tm_response = tm_response.replace("False", "\"False\"")
  98. message_id = int(json.loads(tm_response)["message_id"])
  99. #удаляем следующее сообщение
  100. time.sleep(3)
  101. message_id = message_id + 1
  102. bot.delete_message(chat_id=chat_id, message_id=message_id)
  103. status = "Ok test"
  104. vacancy.canal_city_date = int(time.time())
  105. except:
  106. status = str(traceback.format_exc())
  107. out_s = str(status)
  108. out_s = out_s + "<br>" + tm_response
  109. new_log = Log(
  110. action = "post",
  111. status = status,
  112. time = int(time.time()),
  113. donor = 'hh.ru',
  114. city = city,
  115. specialization = specialization,
  116. vacancies_count = 0,
  117. canal_id = chat_id,
  118. )
  119. session.add(new_log)
  120. session.commit()
  121. start_response('200 OK', [('Content-Type','text/html')])
  122. b = out_s.encode('utf-8')
  123. return [b]