model.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. news_string = news_string.translate(str.maketrans({'_': '\_', '*': '\*', '~': '\~'}))
  35. return news_string
  36. def is_broadcast(cur, this_id):
  37. query = f"""SELECT COUNT(*) FROM human_resource WHERE event = {this_id};"""
  38. res = database_select(cur, query)
  39. # pprint(res)
  40. boolean = True if res[0][0] == 1 else False
  41. return boolean
  42. def record_broadcast(cur, event_id):
  43. query = f"""INSERT INTO human_resource(event) VALUES({event_id});"""
  44. database_insert(cur, query)