model.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. cur.commit()
  26. except (Exception, psycopg2.Error) as error:
  27. print("PostgreSQL error from psycopg2:", error)
  28. def create_links(news_string):
  29. regex = re.compile(r'<a href = "?https?://www\.torn\.com/profiles\.php\?XID=(\d+)"?>([^<]+)</a>')
  30. return_string = re.sub(regex, r'[\2 [\1]](https://www.torn.com/profiles.php?XID=\1)', news_string)
  31. return return_string
  32. def is_broadcast(cur, this_id):
  33. query = f"""SELECT COUNT(*) FROM human_resource WHERE event = {this_id};"""
  34. res = database_select(cur, query)
  35. pprint(res)
  36. boolean = True if res[0][0] == 1 else False
  37. return boolean
  38. def record_broadcast(cur, event_id):
  39. query = f"""INSERT INTO human_resource(event) VALUES({event_id});"""
  40. database_insert(cur, query)