def short(): back_btn = ReturnToMainButton() if back_btn.validate_on_submit(): return redirect('/') long_url = request.args['url'] protocol, long_url = UrlParser.parse(long_url) is_url_valid = UrlVerifier.is_url_valid(protocol, long_url) if not is_url_valid: return redirect_invalid('I will only shorten valid URLs') short_url = request.args['short_url'] if short_url is not '': if url_manager.is_short_url_exists(short_url): return redirect_invalid('Short URL taken') else: url_manager.set_short_url(long_url, short_url) elif short_url is '': short_url = url_manager.get_short_url(long_url) short_url = WEBSITE + short_url return render_template('result.html', title=APP_TITLE, short_url=short_url, back_btn=back_btn)
def read_url(): url = 'https://www.allrecipes.com/recipe/279934/air-fryer-apricot-glazed-chicken-breasts/' up = UrlParser(url) up.extract_recipe()
def test_parse_valid_http_invalid_site(self): result = UrlParser.parse("http://www.randomnonexistentsite.com") self.assertEqual(result, (UrlParser.HTTP, "www.randomnonexistentsite.com"))
def test_parse_invalid(self): result = UrlParser.parse("apples") self.assertEqual(result, (UrlParser.HTTP, "apples"))
def test_parse_valid_no_protocol(self): result = UrlParser.parse("www.google.com") self.assertEqual(result, (UrlParser.HTTP, "www.google.com"))
def test_parse_valid_https(self): result = UrlParser.parse("https://www.google.com") self.assertEqual(result, (UrlParser.HTTPS, "www.google.com"))