Nick_34

Copy the name of the subfolders in all the html files located in all the subfolders

Aug 4th, 2022
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.33 KB | None | 0 0
  1. ---------------------------------
  2. EXPLANATION:
  3.  
  4. ROMANIAN:  https://neculaifantanaru.com/python-copiaza-automat-denumirea-subfolderelor-in-toate-fisierele-html-aflate-in-toate-subfolderele.html
  5.  
  6. ENGLISH:
  7. ---------------------------------
  8.  
  9.  
  10.  
  11. import requests
  12. import re
  13. import os
  14. import regex
  15.  
  16. # folder principal
  17. folder_principal = "e:\\FLAGS"
  18. extension_file = ".html"
  19.  
  20. def read_text_from_file(file_path):
  21.     """
  22.    Aceasta functie returneaza continutul unui fisier.
  23.    file_path: calea catre fisierul din care vrei sa citesti
  24.    """
  25.     with open(file_path, encoding='utf8') as f:
  26.         text = f.read()
  27.         f.close()
  28.         return text
  29.  
  30.  
  31. def write_to_file(text, file_path):
  32.     """
  33.    Aceasta functie scrie un text intr-un fisier.
  34.    text: textul pe care vrei sa il scrii
  35.    file_path: calea catre fisierul in care vrei sa scrii
  36.    """
  37.     with open(file_path, 'wb') as f:
  38.         f.write(text.encode('utf8', 'ignore'))
  39.         f.close()
  40.  
  41. for root, dirs, files in os.walk(folder_principal): # folder principal
  42.     for d in dirs: # subfoldere
  43.         cale_subfolder = root + os.sep + d
  44.         for root_files, dirs_files, files_files in os.walk(cale_subfolder): # fisiere din subfoldere
  45.             for file_name in files_files:
  46.                 file_path = root_files + os.sep + file_name
  47.                 file_content = read_text_from_file(file_path)
  48.  
  49.                 # <meta http-equiv="Content-Language" content="en"/>
  50.                 meta_lang = re.search('<meta http-equiv="Content-Language" content=".*?".*?/>', file_content)
  51.                 if (meta_lang == None):
  52.                     print("Nu am gasit --- en --- in fisierul --- {} ---.".format(file_path))
  53.                 else:
  54.                     meta_lang = meta_lang[0]
  55.                     new_meta_lang = re.sub(r'content=".+"', f'content="{d.lower()}"', meta_lang)
  56.                     file_content = file_content.replace(meta_lang, new_meta_lang)
  57.  
  58.                 # <meta property="og:locale" content="en"/>
  59.                 meta_prop_lang = re.search('<meta property="og:locale" content=".*?".*?/>', file_content)
  60.                 if (meta_prop_lang == None):
  61.                     print("Nu am gasit --- en --- in fisierul --- {} ---.".format(file_path))
  62.                 else:
  63.                     meta_prop_lang = meta_prop_lang[0]
  64.                     new_meta_prop_lang = re.sub(r'content=".+"', f'content="{d.lower()}"', meta_prop_lang)
  65.                     file_content = file_content.replace(meta_prop_lang, new_meta_prop_lang)
  66.  
  67.                 # https://neculaifantanaru.com/en/
  68.                 link_lang_regex = re.compile('https://neculaifantanaru.com/en/')
  69.                 link_lang = re.findall(link_lang_regex, file_content)
  70.                 if (len(link_lang) == 0):
  71.                     print("Nu am gasit --- neculaifantanaru.com/en/ --- in fisierul --- {} ---.".format(file_path))
  72.                 else:
  73.                     for i in range(len(link_lang)):
  74.                         link_lang_i = link_lang[i]
  75.                         new_link_lang_i = re.sub('https://neculaifantanaru.com/en/', f'https://neculaifantanaru.com/{d.lower()}/', link_lang_i)
  76.                         file_content = file_content.replace(link_lang_i, new_link_lang_i)
  77.  
  78.                 # alt="fr" width="28" height="19" /></a>&nbsp; <a href="https://neculaifantanaru.com/af/
  79.                 flags_content = re.search("<!-- FLAGS_1 -->[\s\S]*?<!-- FLAGS -->", file_content)
  80.                 if (flags_content == None):
  81.                     print("Nu am gasit --- FLAGS --- in fisierul --- {} ---.".format(file_path))
  82.                 else:
  83.                     flags_content = flags_content[0]
  84.                     new_flags_content = flags_content
  85.                     links_flags_regex = re.compile('alt="fr" width="28" height="19" /></a>&nbsp; <a href="https://neculaifantanaru.com/.*?/')
  86.                     links_flags = re.findall(links_flags_regex, flags_content)
  87.  
  88.                     if (len(links_flags) == 0):
  89.                         print("Nu am gasit --- links FLAGS --- in fisierul --- {} ---.".format(file_path))
  90.                     else:
  91.                         for i in range(len(links_flags)):
  92.                             link_i = links_flags[i]
  93.                             new_link_i = re.sub('https://neculaifantanaru.com/.*?/', 'https://neculaifantanaru.com/en/', link_i)
  94.                             new_flags_content = new_flags_content.replace(link_i, new_link_i)
  95.                         file_content = file_content.replace(flags_content, new_flags_content)
  96.  
  97.                 # REGEX_1   \x20\|.*(</title>) -> \1
  98.                 title = re.search('\x20\|.*(</title>)', file_content)
  99.                 if (title == None):
  100.                     print("Nu am gasit --- title --- in fisierul --- {} ---.".format(file_path))
  101.                 else:
  102.                     file_content = re.sub(r'\x20\|.*(</title>)', r'\1', file_content)
  103.  
  104.                 # REGEX_2  <td width="149"><div class="rw-ui-container rw-class[\s\S]*?</td>
  105.                 file_content = re.sub(r'<td width="149"><div class="rw-ui-container rw-class[\s\S]*?</td>', '', file_content)
  106.  
  107.                 # REGEX_3  \A(?s).*?<!DOCTYPE html>(?-s).*\R -> \r<!DOCTYPE html>\r
  108.                 file_content = regex.sub(r'[\s\S]*?<\?php[\s\S]*?\?>\s+', r'', file_content)
  109.  
  110.                 write_to_file(file_content, file_path)
  111.  
  112.  
Advertisement
Add Comment
Please, Sign In to add comment