model.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import psycopg2
  2. import re
  3. def get_events():
  4. """Simply retrieve events"""
  5. query = """SELECT * FROM events
  6. WHERE timestamp BETWEEN now() - INTERVAL '24 HOURS' AND now()
  7. AND (news LIKE '% sent an application to join the Faction.'
  8. OR news LIKE '% left the faction.'
  9. OR news LIKE '% has accepted %'
  10. OR news LIKE '% has declined %')
  11. ORDER BY timestamp DESC;"""
  12. return query
  13. def database_select(cur, query):
  14. results = None
  15. try:
  16. cur.execute(query)
  17. results = cur.fetchall()
  18. except (Exception, psycopg2.Error) as error:
  19. print("PostgreSQL error from psycopg2:", error)
  20. return results
  21. def database_insert(cur, query):
  22. try:
  23. cur.execute(query)
  24. cur.commit()
  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. boolean = True if res[0] == 1 else False
  35. return boolean
  36. def record_broadcast(cur, event_id):
  37. query = f"""INSERT INTO human_resource(event) VALUES({event_id});"""
  38. database_insert(cur, query)