Advertisement
mch_pastebin

MessengerCounter-Beta

Aug 17th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.56 KB | None | 0 0
  1. import os
  2. import shutil
  3. import re
  4. from zipfile import ZipFile
  5.  
  6.  
  7. class Person:
  8.     def __init__(self, name):
  9.         self.name = name
  10.         self.messages = []
  11.         self.numberOfCharacters = 0
  12.         self.numberOfMessages = 0
  13.  
  14.  
  15. scriptLocation = os.path.dirname(os.path.realpath(__file__))
  16.  
  17. for file in os.listdir(scriptLocation):
  18.     if file.endswith(".zip"):
  19.         archive = os.path.join(scriptLocation, file)
  20.  
  21. with ZipFile(archive, 'r') as zipFiles:
  22.     zipFiles.extractall('temp')
  23.  
  24. unpackedArchiveLocation = f"{scriptLocation}\\temp"
  25.  
  26. try:
  27.     for fileName in os.listdir(unpackedArchiveLocation):
  28.         with open(f"{unpackedArchiveLocation}\\{fileName}") as file:
  29.             fileContent = file.read()
  30.  
  31.         numberOfPeople = fileName.count('-')
  32.         regexPattern = '(.*).txt'
  33.  
  34.         for i in range(0, numberOfPeople):
  35.             regexPattern = f"(.*)-{regexPattern}"
  36.  
  37.         names = re.search(regexPattern, fileName)
  38.         people = {}
  39.  
  40.         for i in range(1, numberOfPeople + 2):
  41.             people[names.group(i)] = Person(names.group(i))
  42.  
  43.         for line in fileContent.splitlines():
  44.             if people.get(line) is not None:
  45.                 currentPerson = people.get(line)
  46.                 currentPerson.numberOfMessages += 1
  47.                 currentPerson.messages.append('')
  48.             else:
  49.                 currentPerson.numberOfCharacters += len(line)
  50.                 currentPerson.messages[-1] += f"{line}\n"
  51.  
  52.         conversationFolderName = f"Messenger\\{'-'.join(people.keys())}"
  53.         os.makedirs(conversationFolderName, exist_ok=True)
  54.  
  55.         for person in people.values():
  56.             personFolderName = f"{conversationFolderName}\\{person.name}"
  57.             os.makedirs(personFolderName, exist_ok=True)
  58.  
  59.             with open(f"{personFolderName}\\{person.name}-statistics.txt", "w+") as statisticsFile:
  60.                 statisticsFile.write(
  61.                     f"Name: {person.name}\n" +
  62.                     f"Messages: {len(person.messages)}\n" +
  63.                     f"Total characters: {person.numberOfCharacters}\n" +
  64.                     f"Average message length: {person.numberOfCharacters / person.numberOfMessages}\n")
  65.  
  66.             with open(f"{personFolderName}\\{person.name}-messages.txt", "w+") as messagesFile:
  67.                 for i, message in enumerate(person.messages):
  68.                     messagesFile.write(f"{i + 1}:\n{message}\n")
  69. except Exception as exception:
  70.     print(f"Something went wrong\n\nException message: {str(exception)}")
  71. finally:
  72.     shutil.rmtree(unpackedArchiveLocation)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement