forked from L-Dot/Letterboxd-list-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
63 lines (51 loc) · 2.08 KB
/
main.py
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
from list_class import *
from csv_writer import *
'''
Letterboxd List scraper - main program
'''
def main():
print('====================================================')
print('Welcome to the Letterboxd List scraper!')
print('Provided with an URL, this program outputs a CSV file')
print('of movie title, release data and Letterboxd link.')
print('Example url: https://letterboxd.com/.../list/short-films/).')
print('The program currently only supports lists and watchlists.')
print('Enter q or quit to exit the program.')
print('====================================================\n')
# Checking if URL is of a watchlist or of a list
while True:
list_url = input('Enter the URL of the list you wish to scrape:')
# exit option
if list_url == 'q' or list_url == 'quit':
exit()
# if a watchlist proceed this way
elif list_url.split('/')[-2] == 'watchlist':
try:
list_name = list_url.split('/')[-2]
username = list_url.split('/')[-3]
current_list = List(list_name, list_url)
break
except:
print('That is not a valid list URL, please try again.')
continue
# if a list proceed this way
elif list_url.split('/')[-3] == 'list':
try:
list_name = list_url.split('/')[-2]
list_url = list_url + '/detail/' # Adding detail to URL access the personal rating later
current_list = List(list_name, list_url)
break
except:
print('That is not a valid list URL, please try again.')
continue
# writing to a CSV file
try:
csv_name = username + '_' + list_name
print(f'Writing to {csv_name}.csv.')
list_to_csv(current_list.films, csv_name)
except:
print(f'Writing to {list_name}.csv.')
list_to_csv(current_list.films, list_name)
print('Done!')
if __name__ == "__main__":
main()