model.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 is_broadcast(cur, this_id):
  32. query = f"""SELECT COUNT(*) FROM human_resource WHERE event = {this_id};"""
  33. res = database_select(cur, query)
  34. # pprint(res)
  35. boolean = True if res[0][0] == 1 else False
  36. return boolean
  37. def record_broadcast(cur, event_id):
  38. query = f"""INSERT INTO human_resource(event) VALUES({event_id});"""
  39. database_insert(cur, query)