game_bot.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # encoding: utf-8
  2. from datetime import date as dt
  3. # from datetime import datetime as dtm
  4. from datetime import timedelta
  5. from random import randint, choice
  6. from time import sleep
  7. import re
  8. # import logging
  9. from game_bot_logger import exc_logger, selector_logger
  10. import fb_web_bot as fb
  11. # logging.basicConfig(filename='selectors',
  12. # format='%(asctime)s %(message)s', datefmt='%Y-%m-%d', level=logging.INFO)
  13. class StandardList(object):
  14. def __init__(self, perma_list=[]):
  15. self.perma_list = perma_list
  16. def __str__(self):
  17. s = '.'
  18. for ctr, name in enumerate(self.perma_list):
  19. s += '\n' + str(ctr + 1) + ' ' + name
  20. # return object.__str__(self, *args, **kwargs)
  21. return s
  22. def selector(self, k=1, log_file='Game_log/selectors.log') -> str:
  23. c = []
  24. # with open(log_file) as selector_file:
  25. # lines = selector_file.readlines()
  26. # if len(lines) > 0:
  27. # line = lines[-1][11:].strip()
  28. # else:
  29. # line = ''
  30. line = self._past_selectors(log_file=log_file)
  31. while len(c) <= k - 1:
  32. s = choice(self.perma_list)
  33. if s not in c and s not in line: c.append(s)
  34. # logging.info(' VS. '.join(c))
  35. s = ''
  36. for name in range(len(c)):
  37. if name == len(c) - 1:
  38. s += c[name]
  39. else:
  40. s += c[name] + ' VS. '
  41. return s
  42. def _past_selectors(self, log_file=None):
  43. with open(log_file) as selector_file:
  44. lines = selector_file.readlines()
  45. if len(lines) > 0:
  46. last_line = lines[-1][11:].strip()
  47. else:
  48. last_line = ''
  49. self.last_line = last_line
  50. return self.last_line
  51. if __name__ == '__main__':
  52. weekdays = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')
  53. players = ['Tódor B. Ottó', 'Ioan Petrovai', 'Geza Hegyi', 'Levente Marton', 'Ferencz Áron', 'Csaba Kondor', 'Zoltán Gyurka',
  54. 'Simon Arpi', 'Péter Andonia', 'Pantea Róbert', 'Lory Gabor', 'Robert-Ferenc Antal', 'Sipos Lehel', 'Ferenc Jasko']
  55. b_league = 'https://mbasic.facebook.com/groups/106321362854637?refid=18&__tn__=C-R' # ---> original
  56. # b_league = 'https://mbasic.facebook.com/groups/2121044018158040?refid=27' # ---> test
  57. post_id = randint(1000, 7000)
  58. tue = timedelta(days=1)
  59. # text_post = 'foci - {0} - {1} - standard lista'.format(dt.today(), str(dtm.now().time())[:-10])
  60. if dt.today().weekday() + tue.days > 6:
  61. text_post = 'football {0}-{1}-20:30-{2}'.format(weekdays[0], dt.today() + tue, post_id)
  62. else:
  63. text_post = 'football {0}-{1}-20:30-{2}'.format(weekdays[dt.today().weekday() + tue.days], dt.today() + tue, post_id)
  64. sleep_time = 0.5
  65. standard_list = StandardList(perma_list=players)
  66. selectors = standard_list.selector(k=2)
  67. # logging.info(selectors)
  68. selector_logger.info(selectors)
  69. print(str(standard_list))
  70. try:
  71. bot = fb.FacebookBot(visible=False)
  72. bot.login(fb.CREDS['user'], fb.CREDS['pass']); sleep(sleep_time)
  73. bot.goToGroup(b_league); sleep(sleep_time)
  74. bot.postInGroup(b_league, text_post); sleep(sleep_time)
  75. gr_posts = []
  76. while len(gr_posts) == 0:
  77. gr_posts.extend(bot.getPostInGroup(b_league, deep=1))
  78. # gr_posts = bot.getPostInGroup(b_league, deep=1)
  79. sleep(sleep_time)
  80. for post in gr_posts:
  81. # if re.search(str(dtm.now().time().hour) + ':' + str(dtm.now().time().minute), i.text): # and str(dtm.now().time().hour), i.text): # and re.search('foc[a-z]*', i.text): |hr[a-z]* |min[a-z]*
  82. # commented = False
  83. # while not commented:
  84. if re.search(str(post_id), post.text):
  85. print('poster name: {0}, post time: {1}, post text: {2}, num of comm: {3}'
  86. .format(post.posterName, post.time, post.text, post.numComents))
  87. exc_logger.info('poster name: {0}, post time: {1}, post text: {2}'
  88. .format(post.posterName, post.time, post.text))
  89. sleep(sleep_time)
  90. bot.commentInPost(post.linkToComment, str(standard_list))
  91. print('inserted comment'); sleep(sleep_time)
  92. bot.commentInPost(post.linkToComment, selectors)
  93. print('inserted comment'); sleep(sleep_time)
  94. # bot.commentInPost(post.linkToComment, 'Levente Marton -1')
  95. # print('inserted comment')
  96. # sleep(sleep_time)
  97. # bot.commentInPost(post.linkToComment, 'Geza Hegyi -1')
  98. # print('inserted comment')
  99. # sleep(sleep_time)
  100. # commented = True
  101. bot.logout()
  102. sleep(sleep_time * 10)
  103. bot.quit()
  104. print('closed')
  105. exc_logger.info(text_post)
  106. except Exception as exc:
  107. exc_logger.exception(exc)