model.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import psycopg2
  2. import re
  3. # from pprint import pprint
  4. def get_events():
  5. """Simply retrieve events"""
  6. query = """SELECT * FROM
  7. (SELECT * FROM events
  8. WHERE timestamp BETWEEN now() - INTERVAL '24 HOURS' AND now()
  9. AND (news LIKE '% sent an application to join the Faction.'
  10. OR news LIKE '% left the faction.'
  11. OR news LIKE '% has accepted %'
  12. OR news LIKE '% has declined %')
  13. ORDER BY timestamp DESC) tmp
  14. ORDER BY timestamp;"""
  15. return query
  16. def database_select(cur, query):
  17. results = None
  18. try:
  19. cur.execute(query)
  20. results = cur.fetchall()
  21. except (Exception, psycopg2.Error) as error:
  22. print("PostgreSQL error from psycopg2:", error)
  23. return results
  24. def database_insert(cur, query):
  25. try:
  26. cur.execute(query)
  27. except (Exception, psycopg2.Error) as error:
  28. print("PostgreSQL error from psycopg2:", error)
  29. def create_links(news_string):
  30. regex = re.compile(r'<a href = "?https?://www\.torn\.com/profiles\.php\?XID=(\d+)"?>([^<]+)</a>')
  31. return_string = re.sub(regex, r'[\2 [\1]](https://www.torn.com/profiles.php?XID=\1)', news_string)
  32. return return_string
  33. def escape_markdown(news_string):
  34. regex_underscore = re.compile(r'_.+_')
  35. regex_star = re.compile(r'\*.+\*')
  36. regex_tilde = re.compile(r'~.+~')
  37. boolean = re.search(regex_underscore, news_string) or \
  38. re.search(regex_star, news_string) or \
  39. re.search(regex_tilde, news_string)
  40. if boolean:
  41. news_string = news_string.translate(str.maketrans({'_': '\_', '*': '\*', '~': '\~'}))
  42. return news_string
  43. def is_broadcast(cur, this_id):
  44. query = f"""SELECT COUNT(*) FROM human_resource WHERE event = {this_id};"""
  45. res = database_select(cur, query)
  46. # pprint(res)
  47. boolean = True if res[0][0] == 1 else False
  48. return boolean
  49. def record_broadcast(cur, event_id):
  50. query = f"""INSERT INTO human_resource(event) VALUES({event_id});"""
  51. database_insert(cur, query)