# encoding: utf-8 from datetime import date as dt # from datetime import datetime as dtm from datetime import timedelta from random import randint, choice from time import sleep import re # import logging from game_bot_logger import exc_logger, selector_logger import fb_web_bot as fb # logging.basicConfig(filename='selectors', # format='%(asctime)s %(message)s', datefmt='%Y-%m-%d', level=logging.INFO) class StandardList(object): def __init__(self, perma_list=[]): self.perma_list = perma_list def __str__(self): s = '.' for ctr, name in enumerate(self.perma_list): s += '\n' + str(ctr + 1) + ' ' + name # return object.__str__(self, *args, **kwargs) return s def selector(self, k=1, log_file='Game_log/selectors.log') -> str: c = [] # with open(log_file) as selector_file: # lines = selector_file.readlines() # if len(lines) > 0: # line = lines[-1][11:].strip() # else: # line = '' line = self._past_selectors(log_file=log_file) while len(c) <= k - 1: s = choice(self.perma_list) if s not in c and s not in line: c.append(s) # logging.info(' VS. '.join(c)) s = '' for name in range(len(c)): if name == len(c) - 1: s += c[name] else: s += c[name] + ' VS. ' return s def _past_selectors(self, log_file=None): with open(log_file) as selector_file: lines = selector_file.readlines() if len(lines) > 0: last_line = lines[-1][11:].strip() else: last_line = '' self.last_line = last_line return self.last_line if __name__ == '__main__': weekdays = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday') players = ['Tódor B. Ottó', 'Ioan Petrovai', 'Geza Hegyi', 'Levente Marton', 'Ferencz Áron', 'Csaba Kondor', 'Zoltán Gyurka', 'Simon Arpi', 'Péter Andonia', 'Pantea Róbert', 'Lory Gabor', 'Robert-Ferenc Antal', 'Sipos Lehel', 'Ferenc Jasko'] b_league = 'https://mbasic.facebook.com/groups/106321362854637?refid=18&__tn__=C-R' # ---> original # b_league = 'https://mbasic.facebook.com/groups/2121044018158040?refid=27' # ---> test post_id = randint(1000, 7000) tue = timedelta(days=1) # text_post = 'foci - {0} - {1} - standard lista'.format(dt.today(), str(dtm.now().time())[:-10]) if dt.today().weekday() + tue.days > 6: text_post = 'football {0}-{1}-20:30-{2}'.format(weekdays[0], dt.today() + tue, post_id) else: text_post = 'football {0}-{1}-20:30-{2}'.format(weekdays[dt.today().weekday() + tue.days], dt.today() + tue, post_id) sleep_time = 0.5 standard_list = StandardList(perma_list=players) selectors = standard_list.selector(k=2) # logging.info(selectors) selector_logger.info(selectors) print(str(standard_list)) try: bot = fb.FacebookBot(visible=False) bot.login(fb.CREDS['user'], fb.CREDS['pass']); sleep(sleep_time) bot.goToGroup(b_league); sleep(sleep_time) bot.postInGroup(b_league, text_post); sleep(sleep_time) gr_posts = [] while len(gr_posts) == 0: gr_posts.extend(bot.getPostInGroup(b_league, deep=1)) # gr_posts = bot.getPostInGroup(b_league, deep=1) sleep(sleep_time) for post in gr_posts: # 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]* # commented = False # while not commented: if re.search(str(post_id), post.text): print('poster name: {0}, post time: {1}, post text: {2}, num of comm: {3}' .format(post.posterName, post.time, post.text, post.numComents)) exc_logger.info('poster name: {0}, post time: {1}, post text: {2}' .format(post.posterName, post.time, post.text)) sleep(sleep_time) bot.commentInPost(post.linkToComment, str(standard_list)) print('inserted comment'); sleep(sleep_time) bot.commentInPost(post.linkToComment, selectors) print('inserted comment'); sleep(sleep_time) # bot.commentInPost(post.linkToComment, 'Levente Marton -1') # print('inserted comment') # sleep(sleep_time) # bot.commentInPost(post.linkToComment, 'Geza Hegyi -1') # print('inserted comment') # sleep(sleep_time) # commented = True bot.logout() sleep(sleep_time * 10) bot.quit() print('closed') exc_logger.info(text_post) except Exception as exc: exc_logger.exception(exc)