-
Notifications
You must be signed in to change notification settings - Fork 0
/
movies_organizer
executable file
·63 lines (48 loc) · 1.8 KB
/
movies_organizer
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
import os
import shutil
import argparse
import omdb
import re
from requests.exceptions import HTTPError
def title_formater(title):
match = re.search(r"([.':\d\-_\s\w']+)([\._\s]?[\(\[]?\s?[0-9]{4}?\s?[\)\]]?)?", title)
title = re.sub(r'[_]', ' ', match.group(1))
if title.endswith('.'):
title = title[:len(title ) -1]
return title
def get_apikey():
apikey = None
if os.environ.get('OMDB_API_KEY'):
apikey = os.environ.get('OMDB_API_KEY')
if args.apikey:
apikey = args.apikey
return apikey
def organize_movies(path):
movies = os.listdir(path)
os.chdir(path)
for title in movies:
if title in [str(i) for i in range(1, 11)]:
continue
formated_title = title_formater(title)
try:
rating = omdb.title(formated_title)['imdb_rating']
print(f'Moving "{formated_title}" to folder {rating[0]}')
if not os.path.exists(rating[0]):
os.mkdir(rating[0])
shutil.move(title, rating[0])
except KeyError:
print(f'Can\'t find "{title}" in OMDB.')
if '__main__' == __name__:
parser = argparse.ArgumentParser()
parser.add_argument('path', help="Full path for movies folder.")
parser.add_argument("--apikey", help="""You can get omdb api key from this link http://www.omdbapi.com/apikey.aspx.
Or Add OMDB_API_KEY eniroment variable.""")
args = parser.parse_args()
omdb.set_default('apikey', get_apikey())
try:
organize_movies(args.path)
except HTTPError as ex:
print("Error: Can't authorize request you must add --apikey arg or OMDB_API_KEY env var.")
except ConnectionError as ex:
print('Error: you must have internet connection.')