def predict_rotten_tomatoes(movie): link = '' name_split = movie.name.split() for t in name_split: word = [] word_as_list = list(t) # Only add word if alphanumeric for c in word_as_list: if c.isalnum(): c = unidecode(c) c = c.lower() word.append(c) # Covert byte to string word_as_string = ''.join(word) # Rotten Tomatoes URLs are in the format: https://www.rottentomatoes.com/m/word1_word2_word3 # Each word in the title separated by an underscore # No underscore at the end of URL link = ''.join([ link, word_as_string, '_' ]) if len(word) > 0 else ''.join([link, word_as_string]) new_link = urllib.parse.urljoin('https://www.rottentomatoes.com/m/', link[:-1]) if not utils.check_link(new_link): link = ''.join([link[:-1], '_', movie.year]) new_link = urllib.parse.urljoin('https://www.rottentomatoes.com/m/', link) Printer.print_minus(''.join([ "MISSING ROTTEN TOMATOES: ", movie.name, ", Predicted Link: ", new_link ])) return new_link
def add_memes(cog, server_id): name = request.form.get("meme_name") link = request.form.get("meme_link") status = utils.check_link(link) if status == 0: # if is true if name in db.smembers("{}:Memes:name".format(server_id)): flash("This name already exists!", "warning") else: db.hset("{}:Memes:link".format(server_id), name, link) db.sadd("{}:Memes:name".format(server_id), name) flash("You have add a new memes!", "success") return redirect(url_for("memes.memes", server_id=server_id, cog="memes"))
def update_theme(server_id): enable = request.form.get("enable") pic = request.form.get("pic_link") #custom choices border = request.form.get("border") row = request.form.get("row") blur = request.form.get("blur") outlier = request.form.get("outlier") #color col_border = request.form.get("col_border") col_row = request.form.get("col_row") col_outlier = request.form.get("col_outlier") col_text = request.form.get("col_text") if pic != "": status = utils.check_link(pic) else: status = 0 if status == 0: # if is true db.hset("{}:Level:Config".format(server_id),"pic",pic) db.hset("{}:Level:pic_setting".format(server_id),"border",border) db.hset("{}:Level:pic_setting".format(server_id),"row",row) db.hset("{}:Level:pic_setting".format(server_id),"outlier",outlier) db.hset("{}:Level:pic_setting".format(server_id),"blur",blur) if not "" in (col_border,col_row,col_outlier,col_text): for x in [col_border, col_row, col_outlier, col_text]: #just making sure user doesnt enter string value such as white. or we can do edit html that input color? that will change better temp = x.split(",") for check in temp: if check.isdigit() is False: flash("color input cannot have text, it is only value in rgb such as 255,255,255 etc", "warning") return redirect(url_for("level.theme", server_id=server_id)) db.hset("{}:Level:color".format(server_id),"border",col_border) db.hset("{}:Level:color".format(server_id),"row",col_row) db.hset("{}:Level:color".format(server_id),"outlier",col_outlier) db.hset("{}:Level:color".format(server_id),"text",col_text) else: flash("You cannot have blank in color! Please click option above it for not having them","warning") return redirect(url_for("level.theme",server_id = server_id)) if enable is None: db.delete("{}:Level:pic".format(server_id)) else: db.set("{}:Level:pic".format(server_id),enable) flash("Successfully add!","success") return redirect(url_for("level.theme",server_id = server_id))
def edit_memes(server_id, name): new_name = request.form.get("meme_name") link = request.form.get("meme_link") status = utils.check_link(link) if status == 0: # delete old database db.hdel("{}:Memes:link".format(server_id), name) db.srem("{}:Memes:name".format(server_id), name) # adding, if there is a way to rename them in hash, that would be great... db.hset("{}:Memes:link".format(server_id), new_name, link) db.sadd("{}:Memes:name".format(server_id), new_name) flash("Update data!", "success") return redirect(url_for("memes.memes", server_id=server_id, cog="memes"))
def update_theme(server_id): enable = request.form.get("enable") pic = request.form.get("pic_link") #custom choices border = request.form.get("border") row = request.form.get("row") blur = request.form.get("blur") outlier = request.form.get("outlier") #color col_border = request.form.get("col_border") col_row = request.form.get("col_row") col_outlier = request.form.get("col_outlier") col_text = request.form.get("col_text") if pic != "": status = utils.check_link(pic) else: status = 0 if status == 0: # if is true db.hset("{}:Level:Config".format(server_id),"pic",pic) db.hset("{}:Level:pic_setting".format(server_id),"border",border) db.hset("{}:Level:pic_setting".format(server_id),"row",row) db.hset("{}:Level:pic_setting".format(server_id),"outlier",outlier) db.hset("{}:Level:pic_setting".format(server_id),"blur",blur) if not "" in (col_border,col_row,col_outlier,col_text): db.hset("{}:Level:color".format(server_id),"border",col_border) db.hset("{}:Level:color".format(server_id),"row",col_row) db.hset("{}:Level:color".format(server_id),"outlier",col_outlier) db.hset("{}:Level:color".format(server_id),"text",col_text) else: flash("You cannot have blank in color! Please click option above it for not having them","warning") return redirect(url_for("level.theme",server_id = server_id)) if enable is None: db.delete("{}:Level:pic".format(server_id)) else: db.set("{}:Level:pic".format(server_id),enable) flash("Successfully add!","success") return redirect(url_for("level.theme",server_id = server_id))
def web_parse(url=None, config=None, attempts=0, anti_block=False, text=False, html_print=False): """ :param url: url :param config: {configuration} :param attempts: amount of attempts to connect :param anti_block: on/off :param text: convert text to clear text :param html_print: print html from your page :return: ([texts], [links], [theme_tags]) """ if not url: print('[Error] Expected url') return [], [], [], -1 if not config: print('[Error] Expected configuration') return [], [], [], -1 html = 'none' try: if anti_block: headers = { 'User-Agent': f'{utils.get_random_ua()}', 'Referer': urljoin(url, ''), 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' } html = requests.get(url, headers=headers) else: html = requests.get(url) except requests.exceptions.ConnectionError: if attempts < 100: print('Bad connection...') time.sleep(5.0) return web_parse(url=url, config=config, attempts=attempts + 1, anti_block=anti_block) else: print('Cannot to connect to the server') return [], [], [], -1 soup = BeautifulSoup(html.text, 'html.parser') if html_print: print(soup.prettify()) # remove scripts and styles code for script in soup(['script', 'style']): script.extract() raw_texts, links, theme_tags = [], [], [] for tag in soup(): try: # link part if tag.name == 'a': f_func, response = utils.check_link(link=tag, link_config=config['link']) if response: links.append(urljoin(url, tag.get('href'))) # text part else: f_func, response = utils.check_tag(tag=tag, tag_config=config['text']) if response: raw_texts.append(f_func(tag)) except KeyError: print('[Error] Bad configuration') texts = [] if text: for t in raw_texts: response, clear_text = utils.to_text(t) if response: texts.append(clear_text) else: texts = raw_texts return texts, links, theme_tags, 0
def check_url(link, title): if not utils.check_link(link): Printer.print_minus(''.join(["INCORRECT ROTTEN TOMATOES: ", title])) return None return link
def check_url(link, title): if not utils.check_link(link): Printer.print_minus(''.join(["INCORRECT METACRITIC: ", title])) return None return link