Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2017
662
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.09 KB | None | 0 0
  1. import urllib.request
  2. import re
  3. import pickle
  4.  
  5. from bs4 import BeautifulSoup
  6.  
  7. class Dialog:
  8.  
  9.     def __init__(self, speaker, text, season, episode):
  10.  
  11.         self.speaker = speaker
  12.         self.text = text
  13.         self.season = season
  14.         self.episode = episode
  15.  
  16. def update_episode_transcript(url, season, episode):
  17.  
  18.     with urllib.request.urlopen(url) as response:
  19.        html = response.read()
  20.  
  21.     soup = BeautifulSoup(html, "html.parser")
  22.  
  23.     for line in soup.find_all('dd'):
  24.         m = re.search(r":", line.text)
  25.  
  26.         if m:
  27.             character = line.text[:m.end()-1]
  28.             text = line.text[m.end():].replace("\n","")
  29.  
  30.             TRANSCRIPT.append(Dialog(character, text, season, episode))
  31.  
  32.     pickle.dump(TRANSCRIPT, open("TRANSCRIPT.p","wb"))
  33.  
  34. try:
  35.     TRANSCRIPT = pickle.load(open("TRANSCRIPT.p","rb"))
  36. except Exception as e:
  37.     TRANSCRIPT = []
  38.     for season in range(1,8):
  39.  
  40.         print("UPDATING SEASON " + str(season) + " TRANSCRIPTS")
  41.  
  42.         with urllib.request.urlopen("http://mlp.wikia.com/wiki/Category:Season_" + str(season) + "_transcripts") as response:
  43.            html = response.read()
  44.  
  45.         soup = BeautifulSoup(html, "html.parser")
  46.  
  47.         for link in soup.find_all('a'):
  48.             if link.text.startswith("Transcripts/"):
  49.                 update_episode_transcript("http://mlp.wikia.com" + link.get("href"), season, link.text[12:])
  50.                 print("Updated character transcripts for episode: " + link.text[12:])
  51.  
  52.     pickle.dump(TRANSCRIPT, open("TRANSCRIPT.p","wb"))
  53.  
  54. MANE_SIX = {
  55.     "Twilight Sparkle": {"Twilight Sparkle": 0, "Pinkie Pie": 0, "Rarity": 0, "Applejack": 0, "Fluttershy": 0, "Rainbow Dash": 0},
  56.     "Pinkie Pie": {"Twilight Sparkle": 0, "Pinkie Pie": 0, "Rarity": 0, "Applejack": 0, "Fluttershy": 0, "Rainbow Dash": 0},
  57.     "Applejack": {"Twilight Sparkle": 0, "Pinkie Pie": 0, "Rarity": 0, "Applejack": 0, "Fluttershy": 0, "Rainbow Dash": 0},
  58.     "Fluttershy": {"Twilight Sparkle": 0, "Pinkie Pie": 0, "Rarity": 0, "Applejack": 0, "Fluttershy": 0, "Rainbow Dash": 0},
  59.     "Rainbow Dash": {"Twilight Sparkle": 0, "Pinkie Pie": 0, "Rarity": 0, "Applejack": 0, "Fluttershy": 0, "Rainbow Dash": 0},
  60.     "Rarity": {"Twilight Sparkle": 0, "Pinkie Pie": 0, "Rarity": 0, "Applejack": 0, "Fluttershy": 0, "Rainbow Dash": 0}
  61. }
  62.  
  63. NAMES = {
  64.     "Twilight Sparkle": ["twilight"],
  65.     "Pinkie Pie": ["pinkie"],
  66.     "Applejack": ["applejack", "AJ", "A.J. "],
  67.     "Fluttershy": ["fluttershy"],
  68.     "Rainbow Dash": ["dash", "dashie"],
  69.     "Rarity": ["rarity"],
  70. }
  71.  
  72. for line in TRANSCRIPT:
  73.     if line.speaker in MANE_SIX:
  74.         for name in NAMES:
  75.             for nickname in NAMES[name]:
  76.                 if nickname in line.text.replace(",","").lower():
  77.                     MANE_SIX[line.speaker][name] += 1
  78.  
  79. for pony in MANE_SIX:
  80.  
  81.     print(pony.upper() + ":")
  82.  
  83.     for other_pony in MANE_SIX:
  84.         if pony != other_pony:
  85.             print("    Mentions " + other_pony.upper() + " " + str(MANE_SIX[pony][other_pony]) + " times.")
  86.     print("    Mentions herself " + str(MANE_SIX[pony][pony]) + " times.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement