model.py 1.6 KB

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