def run_once(set, outchannel, logging): slack_bot = get_bot(outchannel=='slack') try: data_directory = os.environ['OPENSHIFT_DATA_DIR'] except: data_directory = 'data' spoiled = [] spoilers_to_post = [] new_spoilers = 0 spoiled_file = '{!s}/{!s}.spoiled'.format(data_directory, set) try: with open(spoiled_file) as file: spoiled = json.loads(file.read()) except: pass try: spoilers = spoiler_page_html(set) except: post.write_to_log('Couldn\'t connect to {!s}'.format(spoiler_page(set)), logging=="log") else: for spoiler in spoilers: if spoiler not in spoiled: image_url = '{!s}{!s}'.format(spoiler_page(set), spoiler) spoiled.append(spoiler) spoilers_to_post.append(image_url) new_spoilers += 1 break if new_spoilers > 0: slack_bot.post_images(spoilers_to_post, 'New {!s} spoiler'.format(set.upper())) else: post.write_to_log('No new spoilers right now', logging=="log") try: with open(spoiled_file, 'w+') as file: file.write(json.dumps(spoiled)) except: slack_bot.post_message('I have failed. Dying now.') gone_bad = True
def main(set, outchannel, logging): slack_bot = get_bot(outchannel=='slack') gone_bad = False post.write_to_log('Starting up...', logging=="log") try: data_directory = os.environ['OPENSHIFT_DATA_DIR'] except: data_directory = 'data' while not gone_bad: try: spoiled = [] new_spoilers = 0 spoiled_file = '{!s}/{!s}.spoiled'.format(data_directory, set) try: with open(spoiled_file) as file: spoiled = json.loads(file.read()) except: pass try: spoilers = spoiler_page_html(set) except: post.write_to_log('Couldn\'t connect to {!s}'.format(spoiler_page(set)), logging=="log") else: for spoiler in spoilers: if spoiler not in spoiled: image_url = '{!s}{!s}'.format(spoiler_page(set), spoiler) slack_bot.post_images([image_url], 'New {!s} spoiler'.format(set)) spoiled.append(spoiler) new_spoilers += 1 if new_spoilers == 0: post.write_to_log('No new spoilers right now', logging=="log") try: with open(spoiled_file, 'w+') as file: file.write(json.dumps(spoiled)) except: slack_bot.post_message('I have failed. Dying now.') gone_bad = True except: post.write_to_log('Failed to get spoilers', logging=="log") if not gone_bad: time.sleep(300)
def main(): try: HOST = os.environ['OPENSHIFT_PYTHON_IP'] PORT = int(os.environ['OPENSHIFT_PYTHON_PORT']) write_to_log('Attempting to listen on: {!s}:{!s}'.format(HOST, PORT)) except: HOST = '' PORT = 8080 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) write_to_log('Socket created') try: s.bind((HOST, PORT)) except socket.error as msg: write_to_log('Bind failed. Error: {!s}'.format(msg)) sys.exit() write_to_log('Socket bind complete') s.listen(10) write_to_log('Socket now listening') while 1: conn, addr = s.accept() write_to_log('Connected with {!s}:{!s}'.format(addr[0], addr[1])) s.close()