From c4b5956470f2ce8bd2684d3d4699418783604fb7 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Thu, 11 Oct 2018 20:37:04 +0200 Subject: [PATCH] Created new Function out of duplicated code closes #2 --- riddle.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/riddle.py b/riddle.py index cfd9053..d8a713a 100644 --- a/riddle.py +++ b/riddle.py @@ -20,7 +20,7 @@ hdr: Dict[str, str] = { 'Connection': 'keep-alive'} -async def get_img_as(url): +async def request_soup(url): req = urlreq.Request(url, headers=hdr) html = None for x in range(0, 10): @@ -31,6 +31,11 @@ async def get_img_as(url): print('[-]', e) await asyncio.sleep(1) soup = BeautifulSoup(html, "lxml") + return soup + + +async def get_img_as(url): + soup = await request_soup(url) ret = [] for t in soup.find_all(has_source): if 'redditmedia' not in t['src']: @@ -42,17 +47,8 @@ async def get_img_as(url): async def get_next(url): - req = urlreq.Request(url, headers=hdr) - html = None - for x in range(0, 10): - try: - html = urlreq.urlopen(req).read() - break - except Exception as e: - print('[-]', e) - await asyncio.sleep(1) - soup = BeautifulSoup(html, "lxml") ids = [] + soup = await request_soup(url) for t in soup.find_all(has_source): if 'redditmedia' not in t['src']: try: