nicuf

Keywords+Regex

Jul 8th, 2021 (edited)
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.23 KB | None | 0 0
  1. Explanation here:
  2. Romanian: https://neculaifantanaru.com/regex-python-translate-beautifulsoup-googletrans-html-tags-contains-keywords.html
  3. English: https://neculaifantanaru.com/en/regex-python-translate-beautifulsoup-googletrans-html-tags-contains-keywords.html
  4.  
  5.  
  6.  
  7. from bs4 import BeautifulSoup
  8. from bs4.formatter import HTMLFormatter
  9. from googletrans import Translator
  10. import requests
  11. import re
  12.  
  13. translator = Translator()
  14.  
  15. class UnsortedAttributes(HTMLFormatter):
  16.     def attributes(self, tag):
  17.         for k, v in tag.attrs.items():
  18.             yield k, v
  19.  
  20. files_from_folder = r"c:\Users\Castel\Videos\Captures"
  21.  
  22. use_translate_folder = False
  23.  
  24. destination_language = 'nl'
  25.  
  26. extension_file = ".html"
  27. pattern1 = r'<p class="text_obisnuit">.*(( the | you | which | have | had | then | that | must | make | from | else | does | get | will | make | made | yours | can | your | doesn | their | could | from | at | of | my | an | by | with | are | his | him | she | he | it | may | seem | and | for | else | while | which | be | these | let | ask | has | as | won | keep | but | everything | without | thinking | about | just | to | doesn | if | each | try | I’m | them | one | more | much | on | all | even | over | seems ).*){3,}.*</p>'
  28. pattern2 = r'<p class="text_obisnuit2">.*(( the | you | which | have | had | then | that | must | make | from | else | does | get | will | make | made | yours | can | your | doesn | their | could | from | at | of | my | an | by | with | are | his | him | she | he | it | may | seem | and | for | else | while | which | be | these | let | ask | has | as | won | keep | but | everything | without | thinking | about | just | to | doesn | if | each | try | I’m | them | one | more | much | on | all | even | over | seems ).*){3,}.*</p>'
  29. pattern3 = r'<title>.*(( the | you | which | have | had | then | that | must | make | from | else | does | get | will | make | made | yours | can | your | doesn | their | could | from | at | of | my | an | by | with | are | his | him | she | he | it | may | seem | and | for | else | while | which | be | these | let | ask | has | as | won | keep | but | everything | without | thinking | about | just | to | doesn | if | each | try | I’m | them | one | more | much | on | all | even | over | seems ).*){3,}.*</title>'
  30. pattern4 = r'<meta name="description" content=.*(( the | you | which | have | had | then | that | must | make | from | else | does | get | will | make | made | yours | can | your | doesn | their | could | from | at | of | my | an | by | with | are | his | him | she | he | it | may | seem | and | for | else | while | which | be | these | let | ask | has | as | won | keep | but | everything | without | thinking | about | just | to | doesn | if | each | try | I’m | them | one | more | much | on | all | even | over | seems ).*){3,}.*>'
  31.  
  32. patterns = [pattern1, pattern2, pattern3, pattern4]
  33. import os
  34.  
  35. directory = os.fsencode(files_from_folder)
  36.  
  37. def recursively_translate(node):
  38.     for x in range(len(node.contents)):
  39.         if isinstance(node.contents[x], str):
  40.             if node.contents[x].strip() != '':
  41.                 try:
  42.                     translation = translator.translate(node.contents[x], dest=destination_language).text
  43.                     node.contents[x].replaceWith(translation)
  44.                 except Exception as e:
  45.                     print(e)
  46.         elif node.contents[x] != None:
  47.             recursively_translate(node.contents[x])
  48.  
  49. for file in os.listdir(directory):
  50.     filename = os.fsdecode(file)
  51.     print(filename)
  52.     if filename == 'y_key_e479323ce281e459.html' or filename == 'TS_4fg4_tr78.html':
  53.         continue
  54.     if filename.endswith(extension_file):
  55.         with open(os.path.join(files_from_folder, filename), encoding='utf-8') as html:
  56.             page = html.read()
  57.             updated = False
  58.             for pattern in patterns:
  59.                 for x in re.finditer(pattern, page):
  60.                     updated = True
  61.                     new = x.group(0)
  62.                     soup = BeautifulSoup(new, 'html.parser')
  63.                     if pattern != pattern4:
  64.                         recursively_translate(soup)
  65.                     else:
  66.                         meta = soup.find('meta')
  67.                         meta['content'] = translator.translate(meta['content'], dest=destination_language).text
  68.                     soup = soup.encode(formatter=UnsortedAttributes()).decode('utf-8')
  69.                     page = page.replace(new, soup)
  70.         if updated:
  71.             print(f'{filename} translated')
  72.             new_filename = f'{filename.split(".")[0]}_{destination_language}.html'
  73.             if use_translate_folder:
  74.                 try:
  75.                     with open(os.path.join(files_from_folder+r'\translated', new_filename), 'w', encoding='utf-8') as new_html:
  76.                         new_html.write(page)
  77.                 except:
  78.                     os.mkdir(files_from_folder+r'\translated')
  79.                     with open(os.path.join(files_from_folder+r'\translated', new_filename), 'w', encoding='utf-8') as new_html:
  80.                         new_html.write(page)
  81.             else:
  82.                 with open(os.path.join(files_from_folder, new_filename), 'w', encoding='utf-8') as html:
  83.                     html.write(page)
  84.  
Add Comment
Please, Sign In to add comment