forked from DishantK1807/Python-MiniScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEpisodeRenamer.py
More file actions
47 lines (41 loc) · 1.47 KB
/
EpisodeRenamer.py
File metadata and controls
47 lines (41 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from bs4 import BeautifulSoup
from urllib.request import urlopen
import os
import glob
ext = '.mp4' # File Extension
def main():
url = input('Enter the wiki url for the season: ')
dir = input('Enter the path to the directory: ')
tr = get_table(url)
# Change the root directory of the program to that of the given directory
os.chdir(dir)
# Make a list of all the files with .mp4 file extension
files = glob.glob('*'+ext)
i = 0
for file in files:
# Find Table Heading and get the episode number
th = tr[i].find('th')
epno = th.get('id')
# Find the table row and get the episode name
td = tr[i].find('td')
epname = td.get_text()
# Concatenate and pretty print the name
name = get_name(epno, epname)
# Rename the files in the directory
os.rename(file, name)
i += 1
print('\nAll Files Renamed')
def get_table(url):
"""Find and make a list of the all the episode data"""
soup = BeautifulSoup(urlopen(url), 'html.parser')
table = soup.find('table', 'wikitable')
tr = table.find_all('tr', 'vevent')
return tr
def get_name(epno, epname):
"""Concatenate episode no and name and pretty print it"""
epno = epno[2:] # Remove ep from ep#
# Only take the name between " " and convert it back into string
epname = str((epname.split('\"'))[1])
return epno + ' - ' + epname + ext
if __name__ == '__main__':
main()