eckendoerffer commited on
Commit
50d622e
1 Parent(s): 3f958b2

Update extract_news/2_extract_news.py

Browse files
Files changed (1) hide show
  1. extract_news/2_extract_news.py +3 -7
extract_news/2_extract_news.py CHANGED
@@ -47,6 +47,7 @@ def mysqli_return_number(conn, query):
47
  return result[0] if result else 0
48
 
49
  async def fetch_and_save(url, id_source):
 
50
  try:
51
  async with aiohttp.ClientSession() as session:
52
  async with session.get(url) as response:
@@ -55,10 +56,10 @@ async def fetch_and_save(url, id_source):
55
  text_content = byte_content.decode('utf-8')
56
  except UnicodeDecodeError:
57
  text_content = byte_content.decode('ISO-8859-1')
58
-
59
  with open(f"{path}/sources/html_news/{id_source}.txt", "w", encoding="utf-8") as file:
60
  file.write(text_content)
61
-
 
62
  except aiohttp.client_exceptions.TooManyRedirects:
63
  print(f"Too many redirects for URL: {url}")
64
  except aiohttp.client_exceptions.ClientConnectorError:
@@ -69,24 +70,19 @@ async def fetch_and_save(url, id_source):
69
 
70
  async def main():
71
  conn = mysql.connector.connect(**db_config)
72
-
73
  while True:
74
  time_start = time.time()
75
-
76
  cursor = conn.cursor()
77
  cursor.execute(f"SELECT `id`, `url` FROM `base_news` WHERE `step`='0' ORDER BY RAND() LIMIT {NB_BY_STEP}")
78
  rows = cursor.fetchall()
79
  cursor.close()
80
-
81
  if not rows:
82
  break
83
-
84
  tasks = []
85
  for row in rows:
86
  id_source, url = row
87
  cursor = conn.cursor()
88
  cursor.execute(f"UPDATE `base_news` SET `step`='1' WHERE `id`='{id_source}' LIMIT 1")
89
- print(f'{id_source}) {url}')
90
  cursor.close()
91
  tasks.append(fetch_and_save(url.strip(), id_source))
92
 
 
47
  return result[0] if result else 0
48
 
49
  async def fetch_and_save(url, id_source):
50
+ time_start_item = time.time()
51
  try:
52
  async with aiohttp.ClientSession() as session:
53
  async with session.get(url) as response:
 
56
  text_content = byte_content.decode('utf-8')
57
  except UnicodeDecodeError:
58
  text_content = byte_content.decode('ISO-8859-1')
 
59
  with open(f"{path}/sources/html_news/{id_source}.txt", "w", encoding="utf-8") as file:
60
  file.write(text_content)
61
+ time_end_item = time.time()
62
+ print(f'{id_source}) {time_end_item-time_start_item:.5f} {url}')
63
  except aiohttp.client_exceptions.TooManyRedirects:
64
  print(f"Too many redirects for URL: {url}")
65
  except aiohttp.client_exceptions.ClientConnectorError:
 
70
 
71
  async def main():
72
  conn = mysql.connector.connect(**db_config)
 
73
  while True:
74
  time_start = time.time()
 
75
  cursor = conn.cursor()
76
  cursor.execute(f"SELECT `id`, `url` FROM `base_news` WHERE `step`='0' ORDER BY RAND() LIMIT {NB_BY_STEP}")
77
  rows = cursor.fetchall()
78
  cursor.close()
 
79
  if not rows:
80
  break
 
81
  tasks = []
82
  for row in rows:
83
  id_source, url = row
84
  cursor = conn.cursor()
85
  cursor.execute(f"UPDATE `base_news` SET `step`='1' WHERE `id`='{id_source}' LIMIT 1")
 
86
  cursor.close()
87
  tasks.append(fetch_and_save(url.strip(), id_source))
88