-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.py
1179 lines (1091 loc) · 69.8 KB
/
app.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import streamlit as st
from htbuilder import div, big, h2, styles, p
from htbuilder.units import rem
import altair as alt
import numpy as np
import pandas as pd
import pickle
from deployment import scrape_films_details, scrape_films, scrape_friends, list_friends, recommend_movies, DOMAIN, classify_popularity, classify_likeability, classify_runtime
from pathlib import Path
from datetime import date
from google.oauth2 import service_account
from googleapiclient.discovery import build
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
# Create a connection object.
credentials = service_account.Credentials.from_service_account_info(
st.secrets["gcp_service_account"],
scopes=[
"https://www.googleapis.com/auth/spreadsheets",
],
)
service = build('sheets', 'v4', credentials=credentials)
sheet = service.spreadsheets()
if 'sidebar_state' not in st.session_state:
st.session_state.sidebar_state = 'collapsed'
current_dir = Path(__file__).parent if "__file__" in locals() else Path.cwd()
css_file = current_dir / "styles" / "main.css"
st.set_page_config(page_icon="📽️", page_title="Letterboxd Analysis", layout='wide', initial_sidebar_state=st.session_state.sidebar_state)
with open(css_file) as f:
st.markdown("<style>{}</style>".format(f.read()), unsafe_allow_html=True)
# sections = ['Analyze Profile', 'Compare 2 Profile', 'Friends Ranker + Movie Recommendations']
sections = ['Analyze Profile', 'Friends Ranker + Movie Recommendations']
selected_sect = st.sidebar.selectbox('Choose mode', sections)
mbti_types = ['ENTJ', 'ENFJ', 'ESFJ', 'ESTJ', 'ENTP', 'ENFP', 'ESFP', 'ESTP', 'INTJ', 'INFJ', 'ISFJ', 'ISTJ', 'INTP', 'INFP', 'ISFP', 'ISTP']
if selected_sect == sections[0]:
st.title('📽️ Letterboxd Profile Analyzer')
st.write("""See how you rate your movies, what movies you like, the genres, the actors and directors of those movies 🍿.
Read my **[Medium article](https://medium.com/@alf.19x/letterboxd-profile-analysis-identifying-our-movie-watching-behaviour-281f913a7073)**
about this.""")
st.write("Support me: **[buymeacofee](https://buymeacoffee.com/alfianalytics)**")
with st.expander("ℹ️ What will this app do? (Updated 2023/09/02)"):
st.markdown("""
- Scrape your rated movies
- Scrape your rated movies' details
- Analyze and visualize those movies
""")
st.markdown("""
⚠️ Note: It takes approximately 1 seconds to scrape details from one movie, so it will take some minutes to process
especially when you have rated many movies.
""")
st.markdown("**UPDATE 2023/09/02**")
st.markdown("""
- Added runtime of movies
- Added themes
- Added top list based on standardized calculations for each details
- Added more details""")
row_research = st.columns(2)
with row_research[0]:
with st.expander("ℹ️ Research I'm Doing (PLEASE READ)"):
st.markdown("""
Now I'm doing a personal research on whether MBTI type has effects on someone's favorite genres.
Myers–Briggs Type Indicator (MBTI) is an introspective self-report questionnaire indicating differing psychological preferences in how people perceive the world and make decisions.
So I need your MBTI type and I will take your genres data.
""")
with row_research[1]:
mbti_agree = st.checkbox("I agree to become a respondent for the author's research")
if mbti_agree:
mbti = st.selectbox('MBTI', options=sorted(mbti_types))
username = st.text_input('Letterboxd Username')
row_button = st.columns((6,1,1,6))
submit = row_button[1].button('Submit')
reset = row_button[2].button('Reset')
result = False
if submit:
result = True
if reset:
result = False
if result:
today = date.today()
filename = "{0}_{1}".format(str(today), username)
# df_log = pd.read_csv("log_detail.csv")
result_input = sheet.values().get(spreadsheetId=st.secrets['SAMPLE_SPREADSHEET_ID_input'],
range='log_detail!A:AA').execute()
values_input = result_input.get('values', [])
df_log=pd.DataFrame(values_input[1:], columns=values_input[0])
df_found = df_log[(df_log['date'] == str(today)) & (df_log['username'] == username)].reset_index(drop=True)
if len(df_found) != 1:
# scraping process
df_film = scrape_films(username)
df_film = df_film[df_film['rating']!=-1].reset_index(drop=True)
st.write("You have {0} movies to scrape".format(len(df_film)))
df_rating, df_actor, df_director, df_genre, df_theme = scrape_films_details(df_film, username)
# export file
df_film.to_pickle('log/{0}_dff.pickle'.format(filename))
df_rating.to_pickle('log/{0}_dfr.pickle'.format(filename))
df_actor.to_pickle('log/{0}_dfa.pickle'.format(filename))
df_director.to_pickle('log/{0}_dfd.pickle'.format(filename))
df_genre.to_pickle('log/{0}_dfg.pickle'.format(filename))
df_theme.to_pickle('log/{0}_dft.pickle'.format(filename))
# add new log
new_row = pd.DataFrame({'date':[str(today)], 'username':[username]})
df_log = pd.concat([df_log, new_row]).reset_index(drop=True)
response_date = service.spreadsheets().values().update(
spreadsheetId=st.secrets['SAMPLE_SPREADSHEET_ID_input'],
valueInputOption='RAW',
range='log_detail!A:AA',
body=dict(
majorDimension='ROWS',
values=df_log.T.reset_index().T.values.tolist())
).execute()
else:
st.write("We already have scraped your data today")
df_film = pd.read_pickle('log/{0}_dff.pickle'.format(filename))
df_rating = pd.read_pickle('log/{0}_dfr.pickle'.format(filename))
df_actor = pd.read_pickle('log/{0}_dfa.pickle'.format(filename))
df_director = pd.read_pickle('log/{0}_dfd.pickle'.format(filename))
df_genre = pd.read_pickle('log/{0}_dfg.pickle'.format(filename))
df_theme = pd.read_pickle('log/{0}_dft.pickle'.format(filename))
st.write("---")
st.markdown("<h1 style='text-align:center;'>👤 {0}'s Profile Analysis</h1>".format(username), unsafe_allow_html=True)
st.write('')
row_df = st.columns(3)
with row_df[0]:
st.markdown(
div(
style=styles(
text_align="center",
padding=(rem(1), 0, rem(2), 0),
)
)(
h2(style=styles(font_size=rem(2), padding=0))('👁️ Rated Movies'),
big(style=styles(font_size=rem(5), font_weight=600, line_height=1))(
len(df_film)
)
),
unsafe_allow_html=True,
)
# st.header("⭐ Your Rated Movies")
# st.dataframe(pd.merge(df_film, df_rating)[['title', 'rating', 'avg_rating', 'year']])
with row_df[1]:
st.markdown(
div(
style=styles(
text_align="center",
padding=(rem(1), 0, rem(2), 0),
)
)(
h2(style=styles(font_size=rem(2), padding=0))('❤️ Liked Movies'),
big(style=styles(font_size=rem(5), font_weight=600, line_height=1))(
len(df_film[df_film['liked']==True])
)
),
unsafe_allow_html=True,
)
# st.header("❤️ Your Liked Movies")
# st.dataframe(pd.merge(df_film[df_film['liked']==True], df_rating)[['title', 'rating', 'avg_rating', 'year']])
with row_df[2]:
st.markdown(
div(
style=styles(
text_align="center",
padding=(rem(1), 0, rem(2), 0),
)
)(
h2(style=styles(font_size=rem(2), padding=0))('⭐ Average Ratings'),
big(style=styles(font_size=rem(5), font_weight=600, line_height=1))(
round(df_film['rating'].mean(),2)
)
),
unsafe_allow_html=True,
)
# data_temp = df_film['rating'].astype(str).value_counts().reset_index()
# data_temp.rename(columns = {'index':'rating', 'rating':'count'}, inplace=True)
df_rating['runtime_group'] = df_rating.apply(lambda row:classify_runtime(row['runtime']), axis=1)
df_rating['ltw_ratio'] = df_rating['liked_by']/df_rating['watched_by']
df_rating['popularity'] = df_rating.apply(lambda row: classify_popularity(row['watched_by']), axis=1)
df_rating['likeability'] = df_rating.apply(lambda row: classify_likeability(row['ltw_ratio']), axis=1)
df_rating_merged = pd.merge(df_film, df_rating, left_on='id', right_on='id')
df_rating_merged['rating'] = df_rating_merged['rating'].astype(float)
df_rating_merged['avg_rating'] = df_rating_merged['avg_rating'].astype(float)
df_rating_merged['difference'] = df_rating_merged['rating']-df_rating_merged['avg_rating']
df_rating_merged['difference_abs'] = abs(df_rating_merged['difference'])
st.write("")
row_year = st.columns(2)
with row_year[0]:
st.subheader("When were Your Movies Released?")
st.write("")
st.altair_chart(alt.Chart(df_rating_merged).mark_bar(tooltip=True).encode(
alt.X("year:O", axis=alt.Axis(labelAngle=90)),
y='count()',
color=alt.Color('liked', scale=alt.Scale(domain=[True, False], range=["#ff8000", "#00b020"]))
),
#theme=None,
use_container_width=True
)
st.markdown("""
Looks like the average release date is around **{}**, with your oldest movie being **[{}]({})** ({}) and your latest being **[{}]({})** ({}).
Your movies mostly were released in {}.
""".format(round(df_rating_merged['year'].astype(float).mean()),
df_rating_merged['title'].values[-1], DOMAIN+df_rating_merged['link'].values[-1], df_rating_merged['year'].values[-1],
df_rating_merged['title'].values[0], DOMAIN+df_rating_merged['link'].values[0], df_rating_merged['year'].values[0],
df_rating_merged['year'].value_counts().index[0]
))
with row_year[1]:
st.subheader("Which Decade were Your Movies Released in?")
st.write("")
st.altair_chart(alt.Chart(df_rating_merged).mark_bar(tooltip=True).encode(
alt.X("decade", axis=alt.Axis(labelAngle=0)),
y='count()',
color=alt.Color('liked', scale=alt.Scale(domain=[True, False], range=["#ff8000", "#00b020"]))
),
#theme=None,
use_container_width=True
)
liked = ""
if (df_rating_merged[df_rating_merged['liked'] == True].shape[0] != 0):
liked = """Your favorite decade is probably **{}** since your liked movies mostly were released in that decade, with
{} movies.""".format(df_rating_merged[df_rating_merged['liked'] == True]['decade'].value_counts().index[0],
df_rating_merged[df_rating_merged['liked'] == True]['decade'].value_counts().values[0])
st.markdown("""
You mostly rated movies that were released in the **{}**, you rated {} movies from that decade.
{}
""".format(df_rating_merged['decade'].value_counts().index[0], df_rating_merged['decade'].value_counts().values[0], liked))
st.write("")
st.subheader("How Long are Your Movies?")
row_runtime = st.columns((2,1))
with row_runtime[0]:
st.write("")
st.altair_chart(alt.Chart(df_rating_merged.loc[df_rating_merged['runtime'].notna()]).mark_bar(tooltip=True).encode(
alt.X("runtime_group", axis=alt.Axis(labelAngle=0), sort=["less than 30m", "30m-1h", "1h-1h 30m",
"1h 30m-2h", "2h-2h 30m", "2h 30m-3h",
"at least 3h"]),
y='count()',
color=alt.Color('liked', scale=alt.Scale(domain=[True, False], range=["#ff8000", "#00b020"]))
),
#theme=None,
use_container_width=True
)
with row_runtime[1]:
# st.write(df_rating_merged.loc[df_rating_merged['runtime']==df_rating_merged['runtime'].min(),'title'].values[0])
st.markdown("""
The average runtime of your movies is **{:.2f}** minutes. Your shortest movie is **[{}]({})** with **{:.0f}** minutes, and the longest is
**[{}]({})** with **{:.0f}** minutes.
""".format(df_rating_merged['runtime'].mean(),
df_rating_merged.loc[df_rating_merged['runtime']==df_rating_merged['runtime'].min(),'title'].values[0],
DOMAIN+df_rating_merged.loc[df_rating_merged['runtime']==df_rating_merged['runtime'].min(),'link'].values[0],
df_rating_merged['runtime'].min(),
df_rating_merged.loc[df_rating_merged['runtime']==df_rating_merged['runtime'].max(),'title'].values[0],
DOMAIN+df_rating_merged.loc[df_rating_merged['runtime']==df_rating_merged['runtime'].max(),'link'].values[0],
df_rating_merged['runtime'].max()))
with st.expander('Shortest Movies'):
st.dataframe(df_rating_merged.sort_values('runtime').reset_index(drop=True).shift()[1:].head()[['title','runtime']],
use_container_width=True)
with st.expander('Longest Movies'):
st.dataframe(df_rating_merged.sort_values('runtime',ascending=False).reset_index(drop=True).shift()[1:].head()[['title','runtime']],
use_container_width=True)
st.write("")
row_rating = st.columns(2)
with row_rating[0]:
st.subheader("How Do You Rate Your Movies?")
st.write("")
# st.altair_chart(alt.Chart(data_temp).mark_bar(tooltip=True).encode(
# x='rating',
# y='count',
# color=alt.Color(value="#00b020"),
# ), theme=None, use_container_width=True)
df_film['rating'] = df_film['rating'].astype(str)
st.altair_chart(alt.Chart(df_film).mark_bar(tooltip=True).encode(
alt.X("rating", axis=alt.Axis(labelAngle=0)),
y='count()',
color=alt.Color('liked', scale=alt.Scale(domain=[True, False], range=["#ff8000", "#00b020"]))
),
#theme=None,
use_container_width=True
)
if (df_rating_merged['difference'].mean() > 0):
ave_rat = 'higher'
else:
ave_rat = 'lower'
st.markdown("""
It looks like on average you rated movies **{}** than the average Letterboxd user, **by about {} points**.
You differed from the crowd most on the movie **[{}]({})** where you rated the movie {} stars while the general users rated the movie {}.
""".format(ave_rat, abs(round(df_rating_merged['difference'].mean(),2)),
df_rating_merged[df_rating_merged['difference_abs'] == df_rating_merged['difference_abs'].max()]['title'].values[0],
DOMAIN+df_rating_merged[df_rating_merged['difference_abs'] == df_rating_merged['difference_abs'].max()]['link'].values[0],
df_rating_merged[df_rating_merged['difference_abs'] == df_rating_merged['difference_abs'].max()]['rating'].values[0],
df_rating_merged[df_rating_merged['difference_abs'] == df_rating_merged['difference_abs'].max()]['avg_rating'].values[0]))
with st.expander("Movies You Under Rated"):
st.dataframe(df_rating_merged.sort_values('difference').reset_index(drop=True).shift()[1:].head(5)[['rating','avg_rating','liked','title']],use_container_width=True)
with st.expander("Movies You Over Rated"):
st.dataframe(df_rating_merged.sort_values('difference', ascending=False).reset_index(drop=True).shift()[1:].head(5)[['rating','avg_rating','liked','title']],use_container_width=True)
with row_rating[1]:
st.subheader("How Do Letterboxd Users Rate Your Movies?")
st.write("")
st.altair_chart(alt.Chart(df_rating_merged).mark_bar(tooltip=True).encode(
alt.X("avg_rating", bin=True, axis=alt.Axis(labelAngle=0)),
y='count()',
color=alt.Color('liked', scale=alt.Scale(domain=[True, False], range=["#ff8000", "#00b020"]))
),
#theme=None,
use_container_width=True
)
st.markdown("""
Here is the distribution of average rating by other Letterboxd users for the movies that you've rated. Your movie with the lowest average
rating is **[{}]({})** ({}) with {}, the highest is **[{}]({})** ({}) with {}.
""".format(df_rating_merged[df_rating_merged['avg_rating'] == df_rating_merged['avg_rating'].min()]['title'].values[0],
DOMAIN+df_rating_merged[df_rating_merged['avg_rating'] == df_rating_merged['avg_rating'].min()]['link'].values[0],
df_rating_merged[df_rating_merged['avg_rating'] == df_rating_merged['avg_rating'].min()]['year'].values[0],
df_rating_merged[df_rating_merged['avg_rating'] == df_rating_merged['avg_rating'].min()]['avg_rating'].values[0],
df_rating_merged[df_rating_merged['avg_rating'] == df_rating_merged['avg_rating'].max()]['title'].values[0],
DOMAIN+df_rating_merged[df_rating_merged['avg_rating'] == df_rating_merged['avg_rating'].max()]['link'].values[0],
df_rating_merged[df_rating_merged['avg_rating'] == df_rating_merged['avg_rating'].max()]['year'].values[0],
df_rating_merged[df_rating_merged['avg_rating'] == df_rating_merged['avg_rating'].max()]['avg_rating'].values[0]))
with st.expander("Lowest Rated Movies"):
st.dataframe(df_rating_merged.sort_values('avg_rating').reset_index(drop=True).shift()[1:].head(5)[['rating','avg_rating','liked','title']], use_container_width=True)
with st.expander("Highest Rated Movies"):
st.dataframe(df_rating_merged.sort_values('avg_rating',ascending=False).reset_index(drop=True).shift()[1:].head(5)[['rating','avg_rating','liked','title']], use_container_width=True)
st.write("")
row_popularity = st.columns(2)
with row_popularity[0]:
st.subheader("How Popular are Your Movies?")
st.write("")
st.altair_chart(alt.Chart(df_rating_merged).mark_bar(tooltip=True).encode(
alt.X("popularity", axis=alt.Axis(labelAngle=0)),
y='count()',
color=alt.Color('liked', scale=alt.Scale(domain=[True, False], range=["#ff8000", "#00b020"]))
),
#theme=None,
use_container_width=True
)
popular = ""
if (df_rating_merged['popularity'].value_counts().index[0] in ['3 - popular','4 - very popular']):
popular = "As expected, you mostly rated movies that are popular among Letterboxd users."
else:
popular = "Wow, you have a very unique taste because you mostly don't watch popular movies."
st.markdown("""
{} Your most obscure movie is **[{}]({})** with just **{:,} users watched**, your most popular movie is **[{}]({})** with **{:,} users watched**.
""".format(popular,
df_rating_merged[df_rating_merged['watched_by'] == df_rating_merged['watched_by'].min()]['title'].values[0],
DOMAIN + df_rating_merged[df_rating_merged['watched_by'] == df_rating_merged['watched_by'].min()]['link'].values[0],
df_rating_merged['watched_by'].min(),
df_rating_merged[df_rating_merged['watched_by'] == df_rating_merged['watched_by'].max()]['title'].values[0],
DOMAIN + df_rating_merged[df_rating_merged['watched_by'] == df_rating_merged['watched_by'].max()]['link'].values[0],
df_rating_merged['watched_by'].max()))
with st.expander("Popularity classification"):
st.markdown("""
Popularity is determined by number of watches.
- <= 10,000 -> very obscure
- 10,101 - 100,000 -> obscure
- 100,001 - 1,000,000 -> popular
- \> 1,000,000 -> very popular
""")
with st.expander("Least Popular Movies"):
st.dataframe(df_rating_merged.sort_values('watched_by').reset_index(drop=True).shift()[1:].head(5)[['watched_by','liked','title']], use_container_width=True)
with st.expander("Most Popular Movies"):
st.dataframe(df_rating_merged.sort_values('watched_by', ascending=False).reset_index(drop=True).shift()[1:].head(5)[['watched_by','liked','title']], use_container_width=True)
with row_popularity[1]:
st.subheader("How Likeable are Your Movies?")
st.write("")
st.altair_chart(alt.Chart(df_rating_merged).mark_bar(tooltip=True).encode(
alt.X("likeability", axis=alt.Axis(labelAngle=0)),
y='count()',
color=alt.Color('liked', scale=alt.Scale(domain=[True, False], range=["#ff8000", "#00b020"]))
),
#theme=None,
use_container_width=True
)
unlikeable = ""
if (df_rating_merged[(df_rating_merged['likeability'] == "1 - rarely likeable") & (df_rating_merged['liked'] == True)].shape[0] > 0):
if (df_rating_merged[(df_rating_merged['likeability'] == "1 - rarely likeable") & (df_rating_merged['liked'] == True)].shape[0] > 1):
unlikeable = "Wow, you liked movies that are rarely likeable, you really followed your heart and don't care what others think."
else:
unlikeable = """
Wow, you liked a movie that is rarely likeable, it's **[{}]({}) ({}\% users liked)**, you must have a genuine opinion on this movie.
""".format(df_rating_merged[(df_rating_merged['likeability'] == "1 - rarely likeable") & (df_rating_merged['liked'] == True)]['title'].values[0],
DOMAIN + df_rating_merged[(df_rating_merged['likeability'] == "1 - rarely likeable") & (df_rating_merged['liked'] == True)]['link'].values[0],
round(df_rating_merged[(df_rating_merged['likeability'] == "1 - rarely likeable") & (df_rating_merged['liked'] == True)]['ltw_ratio'].values[0]*100,2))
mostly = ""
if (df_rating_merged['likeability'].value_counts().index[0] == '3 - often likeable'):
mostly = "You mostly rated movies that are often likeable, it possibly means that your movies are mostly good movies."
elif ((df_rating_merged[df_rating_merged['likeability'] == '1 - rarely likeable'].shape[0] +
df_rating_merged[df_rating_merged['likeability'] == '2 - sometimes likeable'].shape[0]) >
(df_rating_merged[df_rating_merged['likeability'] == '3 - often likeable'].shape[0] +
df_rating_merged[df_rating_merged['likeability'] == '4 - usually likeable'].shape[0])):
mostly = "You mostly rated movies that are less likeable, it possibly means that you've been watching bad movies all this time."
st.markdown("""
{} {} Your most likeable movie is **[{}]({})** with **{}\% users liked**, your least likeable movie is **[{}]({})** with just **{}\% users liked**.
""".format(unlikeable, mostly,
df_rating_merged[df_rating_merged['ltw_ratio'] == df_rating_merged['ltw_ratio'].max()]['title'].values[0],
DOMAIN + df_rating_merged[df_rating_merged['ltw_ratio'] == df_rating_merged['ltw_ratio'].max()]['link'].values[0],
round(df_rating_merged['ltw_ratio'].max()*100,2),
df_rating_merged[df_rating_merged['ltw_ratio'] == df_rating_merged['ltw_ratio'].min()]['title'].values[0],
DOMAIN + df_rating_merged[df_rating_merged['ltw_ratio'] == df_rating_merged['ltw_ratio'].min()]['link'].values[0],
round(df_rating_merged['ltw_ratio'].min()*100,2))
)
with st.expander("Likeability classification"):
st.markdown("""
Likeability is determined by number of likes to number of watches ratio.
- <= 0.1 -> rarely likeable
- 0.1 - 0.2 -> sometimes likeable
- 0.2 - 0.4 -> often likeable
- \> 0.4 -> usually likeable
""")
with st.expander("Least Likeable Movies"):
st.dataframe(df_rating_merged.sort_values('ltw_ratio').reset_index(drop=True).shift()[1:].head(5)[['ltw_ratio','title','liked']], use_container_width=True)
with st.expander("Most Likeable Movies"):
st.dataframe(df_rating_merged.sort_values('ltw_ratio', ascending=False).reset_index(drop=True).shift()[1:].head(5)[['ltw_ratio','title','liked']], use_container_width=True)
df_director_merged = pd.merge(df_film, df_director, left_on='id', right_on='id')
df_actor_merged = pd.merge(df_film, df_actor, left_on='id', right_on='id')
df_temp = df_director['director'].value_counts().reset_index()
# df_temp.rename(columns = {'index':'director', 'director':'count'}, inplace=True)
df_director_merged['rating'] = df_director_merged['rating'].astype(float)
df_temp_2 = df_director_merged.groupby(['director', 'director_link']).agg({'liked':'sum', 'rating':'mean'})
df_temp_2 = df_temp_2.reset_index()
# st.dataframe(df_temp_2)
df_temp = pd.merge(df_temp_2, df_temp, left_on='director', right_on='director')
df_temp = df_temp.sort_values(['count','liked','rating'], ascending=False).reset_index(drop=True)
df_temp = df_temp[df_temp['count']!=1]
scaled = scaler.fit_transform(df_temp[['count','liked','rating']].values)
df_weighted = pd.DataFrame(scaled, columns=['count','liked','rating'])
df_weighted = pd.merge(df_temp[['director']], df_weighted, left_index=True, right_index=True)
df_weighted['score'] = df_weighted['count']+df_weighted['liked']+df_weighted['rating']
df_temp = df_temp[df_temp['director'].isin(df_weighted.sort_values('score',ascending=False).head(20)['director'].tolist())]
# n_director = df_temp.iloc[14]['count']
# df_temp = df_temp[df_temp['count']>=n_director]
st.write("")
st.subheader("Your Top Directors")
row_director = st.columns((2,1))
with row_director[0]:
st.write("")
# st.dataframe(df_temp)
base = alt.Chart(df_director_merged[df_director_merged['director'].isin(df_temp['director'])]).encode(
alt.X("director", sort=df_temp['director'].tolist(), axis=alt.Axis(labelAngle=90))
)
area = base.mark_bar(tooltip=True).encode(
alt.Y('count()',
axis=alt.Axis(title='Count of Records')),
color=alt.Color('liked', scale=alt.Scale(domain=[True, False], range=["#ff8000", "#00b020"]))
)
line = alt.Chart(df_temp).mark_line(interpolate='monotone').encode(
alt.X("director", sort=df_temp['director'].tolist(), axis=alt.Axis(labelAngle=90)),
alt.Y('rating', axis=alt.Axis(title='Average Rating', titleColor='#40bcf4'), scale=alt.Scale(zero=False)),
color=alt.Color(value="#40bcf4"),
)
# st.altair_chart(alt.Chart(df_director_merged[df_director_merged['director'].isin(df_temp['director'])]).mark_bar(tooltip=True).encode(
# y=alt.X("director", sort='-x', axis=alt.Axis(labelAngle=0)),
# x='count()',
# color=alt.Color('liked', scale=alt.Scale(domain=[True, False], range=["#ff8000", "#00b020"]))
# ), theme=None, use_container_width=True)
# st.altair_chart(alt.Chart(df_temp).mark_line().encode(
# alt.Y("director", sort=df_temp['director'].tolist(), axis=alt.Axis(labelAngle=0)),
# alt.X('rating', scale=alt.Scale(zero=False)),
# color=alt.Color(value="#00b020"),
# ), theme=None, use_container_width=True)
st.altair_chart(alt.layer(area, line).resolve_scale(
y = 'independent'
),
#theme=None,
use_container_width=True
)
with row_director[1]:
if (df_temp['liked'].max() != 0):
if (df_temp[df_temp['rating']==df_temp['rating'].max()]['director'].values[0] != df_temp[df_temp['liked']==df_temp['liked'].max()]['director'].values[0]):
st.markdown("""
You rated **{}** movies that were directed by **[{}]({})**. Your favorite director is probably **[{}]({})** which you
gave average rating of **{}**, or **[{}]({})** which you liked **{}** of his/her movies.
""".format(df_temp['count'].values[0], df_temp['director'].values[0], DOMAIN+df_temp['director_link'].values[0],
df_temp[df_temp['rating']==df_temp['rating'].max()]['director'].values[0],
DOMAIN+df_temp[df_temp['rating']==df_temp['rating'].max()]['director_link'].values[0],
round(df_temp['rating'].max(), 2),
df_temp[df_temp['liked']==df_temp['liked'].max()]['director'].values[0],
DOMAIN+df_temp[df_temp['liked']==df_temp['liked'].max()]['director_link'].values[0],
df_temp['liked'].max()))
else:
st.markdown("""
You rated **{}** movies that were directed by **[{}]({})**. Your favorite director is probably **[{}]({})** which you
gave average rating of **{}** and liked **{}** of his/her movies.
""".format(df_temp['count'].values[0], df_temp['director'].values[0], DOMAIN+df_temp['director_link'].values[0],
df_temp[df_temp['rating']==df_temp['rating'].max()]['director'].values[0],
DOMAIN+df_temp[df_temp['rating']==df_temp['rating'].max()]['director_link'].values[0],
round(df_temp['rating'].max(), 2),
df_temp['liked'].max()))
else:
st.markdown("""
You rated **{}** movies that were directed by **[{}]({})**. Your favorite director is probably **[{}]({})** which you
gave average rating of **{}**.
""".format(df_temp['count'].values[0], df_temp['director'].values[0], DOMAIN+df_temp['director_link'].values[0],
df_temp[df_temp['rating']==df_temp['rating'].max()]['director'].values[0],
DOMAIN+df_temp[df_temp['rating']==df_temp['rating'].max()]['director_link'].values[0],
round(df_temp['rating'].max(), 2)))
st.markdown("""
Based on standardized calculations:
1. {}
2. {}
3. {}
4. {}
5. {}
""".format(
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[0,'director'],
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[1,'director'],
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[2,'director'],
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[3,'director'],
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[4,'director'],
))
# st.dataframe(df_weighted.sort_values('score',ascending=False).head())
list_weights = []
movie_ids = df_actor['id'].unique()
for movie_id in movie_ids:
n_actor = df_actor.loc[df_actor['id']==movie_id].shape[0]
for i in range(n_actor):
weight = 1-i/n_actor
list_weights.append(weight)
df_actor['weights'] = list_weights
df_temp_w = df_actor.groupby(['actor', 'actor_link'],as_index=False)['weights'].sum()
df_temp = df_actor['actor'].value_counts().reset_index()
# df_temp.rename(columns = {'index':'actor', 'actor':'count'}, inplace=True)
df_actor_merged['rating'] = df_actor_merged['rating'].astype(float)
df_temp_2 = df_actor_merged.groupby(['actor', 'actor_link']).agg({'liked':'sum', 'rating':'mean'})
df_temp_2 = df_temp_2.reset_index()
df_temp = pd.merge(df_temp_2, df_temp, left_on='actor', right_on='actor')
df_temp = pd.merge(df_temp, df_temp_w, left_on=['actor','actor_link'], right_on=['actor','actor_link'])
df_temp = df_temp.sort_values(['count','liked','rating'], ascending=False).reset_index(drop=True)
df_temp = df_temp[df_temp['count']!=1]
df_temp['liked_weighted'] = df_temp['liked'].astype(int)*df_temp['weights']
scaled = scaler.fit_transform(df_temp[['weights','liked_weighted','rating']].values)
df_weighted = pd.DataFrame(scaled, columns=['weights','liked_weighted','rating'])
df_weighted = pd.merge(df_temp[['actor']], df_weighted, left_index=True, right_index=True)
df_weighted['score'] = df_weighted['weights']+df_weighted['liked_weighted']+df_weighted['rating']
df_temp = df_temp[df_temp['actor'].isin(df_weighted.sort_values('score',ascending=False).head(20)['actor'].tolist())]
# n_actor = df_temp.iloc[19]['count']
# df_temp = df_temp[df_temp['count']>=n_actor]
# df_temp = df_temp[:10]
st.write("")
st.subheader("Your Top Actors")
row_actor = st.columns((2,1))
with row_actor[0]:
st.write("")
# st.altair_chart(alt.Chart(df_actor_merged[df_actor_merged['actor'].isin(df_temp['actor'])]).mark_bar(tooltip=True).encode(
# y=alt.X("actor", sort='-x', axis=alt.Axis(labelAngle=0)),
# x='count()',
# color=alt.Color('liked', scale=alt.Scale(domain=[True, False], range=["#ff8000", "#00b020"]))
# ), theme=None, use_container_width=True)
base = alt.Chart(df_actor_merged[df_actor_merged['actor'].isin(df_temp['actor'])]).encode(
alt.X("actor", sort=df_temp['actor'].tolist(), axis=alt.Axis(labelAngle=90))
)
area = base.mark_bar(tooltip=True).encode(
alt.Y('count()',
axis=alt.Axis(title='Count of Records')),
color=alt.Color('liked', scale=alt.Scale(domain=[True, False], range=["#ff8000", "#00b020"]))
)
line = alt.Chart(df_temp).mark_line(interpolate='monotone').encode(
alt.X("actor", sort=df_temp['actor'].tolist(), axis=alt.Axis(labelAngle=90)),
alt.Y('rating', axis=alt.Axis(title='Average Rating', titleColor='#40bcf4'), scale=alt.Scale(zero=False)),
color=alt.Color(value="#40bcf4"),
)
st.altair_chart(alt.layer(area, line).resolve_scale(
y = 'independent'
),
#theme=None,
use_container_width=True
)
with row_actor[1]:
if (df_temp['liked'].max() != 0):
st.markdown("""
You rated **{}** movies starring **[{}]({})**. Your favorite actor is probably **[{}]({})** which you liked **{}** of
his/her movies.
""".format(df_temp['count'].values[0], df_temp['actor'].values[0], DOMAIN+df_temp['actor_link'].values[0],
df_temp[df_temp['liked']==df_temp['liked'].max()]['actor'].values[0],
DOMAIN+df_temp[df_temp['liked']==df_temp['liked'].max()]['actor_link'].values[0],
df_temp['liked'].max()))
else:
st.markdown("""
You rated **{}** movies starring **[{}]({})**.
""".format(df_temp['count'].values[0], df_temp['actor'].values[0], DOMAIN+df_temp['actor_link'].values[0]))
st.markdown("""
Based on standardized and weighted calculations:
1. {}
2. {}
3. {}
4. {}
5. {}
6. {}
7. {}
8. {}
9. {}
10. {}
""".format(
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[0,'actor'],
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[1,'actor'],
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[2,'actor'],
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[3,'actor'],
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[4,'actor'],
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[5,'actor'],
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[6,'actor'],
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[7,'actor'],
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[8,'actor'],
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[9,'actor']
))
st.write("")
st.subheader("Genres Breakdown")
row_genre = st.columns((2,1))
df_genre_merged = pd.merge(df_film, df_genre, left_on='id', right_on='id')
df_temp = df_genre['genre'].value_counts().reset_index()
# df_temp.rename(columns = {'index':'genre', 'genre':'count'}, inplace=True)
df_temp = df_temp[df_temp['count'] > df_film.shape[0]/100].reset_index(drop=True)
df_genre_merged['rating'] = df_genre_merged['rating'].astype(float)
df_temp_2 = df_genre_merged.groupby(['genre']).agg({'liked':'sum', 'rating':'mean'})
df_temp_2 = df_temp_2.reset_index()
df_temp = pd.merge(df_temp_2, df_temp, left_on='genre', right_on='genre')
df_temp = df_temp.sort_values('count', ascending=False).reset_index(drop=True)
scaled = scaler.fit_transform(df_temp[['count','liked','rating']].values)
df_weighted = pd.DataFrame(scaled, columns=['count','liked','rating'])
df_weighted = pd.merge(df_temp[['genre']], df_weighted, left_index=True, right_index=True)
df_weighted['score'] = df_weighted['count']+df_weighted['liked']+df_weighted['rating']
if mbti_agree:
result_input = sheet.values().get(spreadsheetId=st.secrets['SAMPLE_SPREADSHEET_ID_input'],
range='mbti!A:AA').execute()
values_input = result_input.get('values', [])
df_log_mbti=pd.DataFrame(values_input[1:], columns=values_input[0])
df_mbti_genre = df_weighted.copy()
df_mbti_genre = df_mbti_genre.sort_values('score',ascending=False).head()
df_mbti_genre['mbti'] = mbti
df_mbti_genre['username'] = username
df_mbti_genre = df_mbti_genre[['username', 'mbti', 'genre', 'score']]
df_log_mbti = pd.concat([df_log_mbti, df_mbti_genre]).reset_index(drop=True)
df_log_mbti.drop_duplicates(['username','mbti','genre'], inplace=True)
response_date = service.spreadsheets().values().update(
spreadsheetId=st.secrets['SAMPLE_SPREADSHEET_ID_input'],
valueInputOption='RAW',
range='mbti!A:AA',
body=dict(
majorDimension='ROWS',
values=df_log_mbti.T.reset_index().T.values.tolist())
).execute()
with row_genre[0]:
st.write("")
# st.altair_chart(alt.Chart(df_genre_merged).mark_bar(tooltip=True).encode(
# alt.X("genre", sort='-y', axis=alt.Axis(labelAngle=45)),
# y='count()',
# color=alt.Color('liked', scale=alt.Scale(domain=[True, False], range=["#ff8000", "#00b020"]))
# ), theme=None, use_container_width=True)
# st.altair_chart(alt.Chart(df_genre_merged).mark_line(tooltip=True).encode(
# alt.X("genre", sort='-y', axis=alt.Axis(labelAngle=45)),
# alt.Y('mean(rating):Q', scale=alt.Scale(zero=False)),
# color=alt.Color(value="#00b020"),
# ), theme=None, use_container_width=True)
base = alt.Chart(df_genre_merged[df_genre_merged['genre'].isin(df_temp['genre'])]).encode(
alt.X("genre", sort=df_temp['genre'].tolist(), axis=alt.Axis(labelAngle=90))
)
# st.altair_chart(base)
area = base.mark_bar(tooltip=True).encode(
alt.Y('count()',
axis=alt.Axis(title='Count of Records')),
color=alt.Color('liked', scale=alt.Scale(domain=[True, False], range=["#ff8000", "#00b020"]))
)
line = alt.Chart(df_temp).mark_line(interpolate='monotone').encode(
alt.X('genre', sort=df_temp['genre'].tolist()),
alt.Y('rating',
axis=alt.Axis(title='Average Rating', titleColor='#40bcf4'), scale=alt.Scale(zero=False)),
color=alt.Color(value="#40bcf4")
)
# line = base.mark_line(tooltip=True).encode(
# alt.Y('mean(rating):Q',
# axis=alt.Axis(title='Average Rating'), scale=alt.Scale(zero=False)),
# color=alt.Color(value="#ff8000")
# )
st.altair_chart(alt.layer(area, line).resolve_scale(
y = 'independent'
),
#theme=None,
use_container_width=True
)
with row_genre[1]:
liked = ""
if (df_temp['liked'].max() != 0):
liked = "You mostly liked **{}** movies with {} movies.".format(df_temp[df_temp['liked']==df_temp['liked'].max()]['genre'].values[0],
df_temp[df_temp['liked']==df_temp['liked'].max()]['liked'].values[0])
st.markdown("""
Seems like you're not a great fan of **{}** movies, you gave average rating of {} on that genre.
You really gave good ratings on **{}** movies, with average rating of {}.
You mostly rated **{}** movies with {} movies. {}
""".format(df_temp[df_temp['rating']==df_temp['rating'].min()]['genre'].values[0],
round(df_temp[df_temp['rating']==df_temp['rating'].min()]['rating'].values[0], 2),
df_temp[df_temp['rating']==df_temp['rating'].max()]['genre'].values[0],
round(df_temp[df_temp['rating']==df_temp['rating'].max()]['rating'].values[0], 2),
df_temp[df_temp['count']==df_temp['count'].max()]['genre'].values[0],
df_temp[df_temp['liked']==df_temp['liked'].max()]['count'].values[0],
liked))
st.markdown("""
Based on standardized calculations:
1. {}
2. {}
3. {}
4. {}
5. {}
""".format(
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[0,'genre'],
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[1,'genre'],
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[2,'genre'],
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[3,'genre'],
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[4,'genre'],
))
df_genre_combination = pd.DataFrame(columns=df_genre_merged.columns)
for i in range(len(df_temp['genre'].tolist())):
for j in range(i+1, len(df_temp['genre'].tolist())):
df_ha = df_genre_merged[(df_genre_merged['genre'] == df_temp['genre'].tolist()[i]) | (df_genre_merged['genre'] == df_temp['genre'].tolist()[j])]
if len(df_ha) != 0:
df_ha['genre'] = df_temp['genre'].tolist()[i] + " & " + df_temp['genre'].tolist()[j]
df_ha = df_ha[df_ha.duplicated('id')]
df_genre_combination = pd.concat([df_genre_combination, df_ha]).reset_index(drop=True)
df_temp_comb = df_genre_combination['genre'].value_counts().reset_index()
# df_temp_comb.rename(columns = {'index':'genre', 'genre':'count'}, inplace=True)
df_genre_combination['rating'] = df_genre_combination['rating'].astype(float)
df_genre_combination['liked'] = df_genre_combination['liked'].astype(int)
df_temp_comb_2 = df_genre_combination.groupby(['genre']).agg({'liked':'sum', 'rating':'mean'})
df_genre_combination['liked'] = df_genre_combination['liked'].astype(bool)
df_temp_comb_2 = df_temp_comb_2.reset_index()
df_temp_comb = pd.merge(df_temp_comb_2, df_temp_comb, left_on='genre', right_on='genre')
df_temp_comb = df_temp_comb.sort_values('count', ascending=False).reset_index(drop=True)
scaled = scaler.fit_transform(df_temp_comb[['count','liked','rating']].values)
df_weighted = pd.DataFrame(scaled, columns=['count','liked','rating'])
df_weighted = pd.merge(df_temp_comb[['genre']], df_weighted, left_index=True, right_index=True)
df_weighted['score'] = df_weighted['count']+df_weighted['liked']+df_weighted['rating']
n_genre = df_temp_comb.iloc[19]['count']
df_temp_comb = df_temp_comb[df_temp_comb['count']>=n_genre]
st.subheader("Top Genre Combinations Breakdown")
row_genre_comb = st.columns((2,1))
with row_genre_comb[0]:
st.write("")
base = alt.Chart(df_genre_combination[df_genre_combination['genre'].isin(df_temp_comb['genre'])]).encode(
alt.X("genre", sort=df_temp_comb['genre'].tolist(), axis=alt.Axis(labelAngle=90))
)
area = base.mark_bar(tooltip=True).encode(
alt.Y('count()',
axis=alt.Axis(title='Count of Records')),
color=alt.Color('liked', scale=alt.Scale(domain=[True, False], range=["#ff8000", "#00b020"]))
)
line = alt.Chart(df_temp_comb).mark_line(interpolate='monotone').encode(
alt.X('genre', axis=alt.Axis(title='genre combination'), sort=df_temp_comb['genre'].tolist()),
alt.Y('rating',
axis=alt.Axis(title='Average Rating', titleColor='#40bcf4'), scale=alt.Scale(zero=False)),
color=alt.Color(value="#40bcf4")
)
st.altair_chart(alt.layer(area, line).resolve_scale(
y = 'independent'
),
#theme=None,
use_container_width=True
)
with row_genre_comb[1]:
top_2 = ""
if (pd.DataFrame(df_temp_comb['genre'][0].split(" & ")).isin(df_temp.iloc[:2]['genre'].tolist()).sum()[0] == 0):
top_2 = """
It's a little bit surprising that your mostly rated genre combination (**{}**) is not your top 2 genres (**{} & {}**).
""".format(df_temp_comb['genre'][0], df_temp['genre'][0], df_temp['genre'][1])
elif ((pd.DataFrame(df_temp_comb['genre'][0].split(" & ")).isin(df_temp.iloc[:2]['genre'].tolist()).sum()[0] == 1)):
top_2 = "Well, it's no surprise that your mostly rated genre combination (**{}**) consists of one of your top 2 genres (**{}**).".format(df_temp_comb['genre'][0],
df_temp.iloc[:2][df_temp.iloc[:2]['genre'].isin(df_temp_comb['genre'][0].split(" & "))]['genre'].values[0])
elif ((pd.DataFrame(df_temp_comb['genre'][0].split(" & ")).isin(df_temp.iloc[:2]['genre'].tolist()).sum()[0] == 2)):
top_2 = "Well, it's no surprise that your mostly rated genre combination consists of your top 2 genres (**{}**).".format(df_temp_comb['genre'][0])
st.markdown("""It's a common thing that a movie is categorized into more than 1 genre, so we'll look deeper into the genre combinations
to get a better understanding of your movies.
""")
low = ""
if (pd.DataFrame(df_temp_comb[df_temp_comb['rating'] == df_temp_comb['rating'].min()]['genre'].values[0].split(" & ")).isin(df_temp[df_temp['rating'] == df_temp['rating'].min()]['genre'].values.tolist()).sum()[0] != 0):
low = """Once again, **{}** movies are definitely not your cup of tea, even when it's combined with other genre, the combination of **{}**
has the lowest average rating ({}) compared to your other top genre combinations.
""".format(df_temp[df_temp['rating'] == df_temp['rating'].min()]['genre'].values[0],
df_temp_comb[df_temp_comb['rating'] == df_temp_comb['rating'].min()]['genre'].values[0],
round(df_temp_comb['rating'].min(),2))
else:
low = """Genre combination with the lowest average rating you gave among your other top genre combinations is **{}** with {}.
""".format(df_temp_comb[df_temp_comb['rating'] == df_temp_comb['rating'].min()]['genre'].values[0],
round(df_temp_comb['rating'].min(),2))
high = ""
if (pd.DataFrame(df_temp_comb[df_temp_comb['rating'] == df_temp_comb['rating'].max()]['genre'].values[0].split(" & ")).isin(df_temp[df_temp['rating'] == df_temp['rating'].max()]['genre'].values.tolist()).sum()[0] != 0):
high = """You seem to have a lot appreciation for **{}** movies, the combination of **{}**
has the highest average rating ({}) compared to your other top genre combinations.
""".format(df_temp[df_temp['rating'] == df_temp['rating'].max()]['genre'].values[0],
df_temp_comb[df_temp_comb['rating'] == df_temp_comb['rating'].max()]['genre'].values[0],
round(df_temp_comb['rating'].max(),2))
else:
high = """You gave the highest average rating to **{}** movies with {}.
""".format(df_temp_comb[df_temp_comb['rating'] == df_temp_comb['rating'].max()]['genre'].values[0],
round(df_temp_comb['rating'].max(),2))
st.markdown("{} {} {}".format(top_2, low, high))
st.markdown("""
Based on standardized calculations:
1. {}
2. {}
3. {}
4. {}
5. {}
""".format(
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[0,'genre'],
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[1,'genre'],
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[2,'genre'],
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[3,'genre'],
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[4,'genre'],
))
# st.dataframe(df_rating_merged)
df_theme_merged = pd.merge(df_film, df_theme, left_on='id', right_on='id')
df_temp = df_theme['theme'].value_counts().reset_index()
# df_temp.rename(columns = {'index':'theme', 'theme':'count'}, inplace=True)
df_theme_merged['rating'] = df_theme_merged['rating'].astype(float)
df_temp_2 = df_theme_merged.groupby(['theme']).agg({'liked':'sum', 'rating':'mean'})
df_temp_2 = df_temp_2.reset_index()
df_temp = pd.merge(df_temp_2, df_temp, left_on='theme', right_on='theme')
df_temp = df_temp.sort_values(['count','liked','rating'], ascending=False).reset_index(drop=True)
scaled = scaler.fit_transform(df_temp[['count','liked','rating']].values)
df_weighted = pd.DataFrame(scaled, columns=['count','liked','rating'])
df_weighted = pd.merge(df_temp[['theme']], df_weighted, left_index=True, right_index=True)
df_weighted['score'] = df_weighted['count']+df_weighted['liked']+df_weighted['rating']
n_theme = df_temp.iloc[19]['count']
df_temp = df_temp[df_temp['count']>=n_theme]
if mbti_agree:
result_input = sheet.values().get(spreadsheetId=st.secrets['SAMPLE_SPREADSHEET_ID_input'],
range='mbti_theme!A:AA').execute()
values_input = result_input.get('values', [])
df_log_mbti=pd.DataFrame(values_input[1:], columns=values_input[0])
df_mbti_theme = df_weighted.copy()
df_mbti_theme = df_mbti_theme.sort_values('score',ascending=False).head()
df_mbti_theme['mbti'] = mbti
df_mbti_theme['username'] = username
df_mbti_theme = df_mbti_theme[['username', 'mbti', 'theme', 'score']]
df_log_mbti = pd.concat([df_log_mbti, df_mbti_theme]).reset_index(drop=True)
df_log_mbti.drop_duplicates(['username','mbti','theme'], inplace=True)
response_date = service.spreadsheets().values().update(
spreadsheetId=st.secrets['SAMPLE_SPREADSHEET_ID_input'],
valueInputOption='RAW',
range='mbti_theme!A:AA',
body=dict(
majorDimension='ROWS',
values=df_log_mbti.T.reset_index().T.values.tolist())
).execute()
# df_temp = df_temp[df_temp['count']!=1]
st.write("")
st.subheader("Top Themes")
row_theme = st.columns((2,1))
with row_theme[0]:
st.write("")
# st.dataframe(df_temp)
base = alt.Chart(df_theme_merged[df_theme_merged['theme'].isin(df_temp['theme'])]).encode(
alt.X("theme", sort=df_temp['theme'].tolist(), axis=alt.Axis(labelAngle=90))
)
area = base.mark_bar(tooltip=True).encode(
alt.Y('count()',
axis=alt.Axis(title='Count of Records')),
color=alt.Color('liked', scale=alt.Scale(domain=[True, False], range=["#ff8000", "#00b020"]))
)
line = alt.Chart(df_temp).mark_line(interpolate='monotone').encode(
alt.X("theme", sort=df_temp['theme'].tolist(), axis=alt.Axis(labelAngle=90)),
alt.Y('rating', axis=alt.Axis(title='Average Rating', titleColor='#40bcf4'), scale=alt.Scale(zero=False)),
color=alt.Color(value="#40bcf4"),
)
st.altair_chart(alt.layer(area, line).resolve_scale(
y = 'independent'
),
#theme=None,
use_container_width=True
)
with row_theme[1]:
liked = ""
if (df_temp['liked'].max() != 0):
if df_temp[df_temp['liked']==df_temp['liked'].max()]['theme'].values[0]==df_temp[df_temp['count']==df_temp['count'].max()]['theme'].values[0]:
liked = liked = "Your most watched and liked theme is **{}**.".format(
df_temp[df_temp['liked']==df_temp['liked'].max()]['theme'].values[0])
else:
liked = "Your most liked theme is **{}**.".format(
df_temp[df_temp['liked']==df_temp['liked'].max()]['theme'].values[0])
ratings = """
You don't seem to enjoy movies with **{}** theme since you rated it the lowest. Conversely, you gave relatively high ratings on movies with **{}** theme.
""".format(df_temp[df_temp['rating']==df_temp['rating'].min()]['theme'].values[0],
df_temp[df_temp['rating']==df_temp['rating'].max()]['theme'].values[0])
st.markdown("{} {}".format(liked, ratings))
st.markdown("""
Based on standardized calculations:
1. {}
2. {}
3. {}
4. {}
5. {}
""".format(
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[0,'theme'],
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[1,'theme'],
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[2,'theme'],
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[3,'theme'],
df_weighted.sort_values('score',ascending=False).reset_index(drop=True).loc[4,'theme']
))
# st.dataframe(df_weighted.sort_values('score',ascending=False).head())
# elif selected_sect == sections[1]:
# st.write("Still not ready hehe")
elif selected_sect == sections[1]:
st.title('📽️ Letterboxd Friends Ranker (+ Movie Recommendations)')
st.write("""See which friend has the most similar taste in movies to yours based on the ratings and likes of the movies you
both have watched 🍿. Read my **[Medium article](https://medium.com/@alf.19x/letterboxd-friends-ranker-simple-movie-recommendation-system-80a38dcfb0da)**
about this.""")
st.write("Support me: **[buymeacofee](https://buymeacoffee.com/alfianalytics)**")
with st.expander("ℹ️ What will this app do?"):
st.markdown("""
- Scrape your rated movies
- Scrape your friends
- Scrape your friends' rated movies
- Compute similarity between you and each of your friend
- Rank the similarity score
- Make movie recommendations based on your friends' movies
""")
st.markdown("""
⚠️ Note: It takes approximately 10 seconds to scrape 400 movies from one Letterboxd profile,
so if you have many friends and they have watched many movies, it will take some minutes to process.
""")
row0 = st.columns(3)
with row0[0]:
username = st.text_input('Letterboxd Username')
with row0[1]:
ftype = st.selectbox('Friends Category', options=['following', 'followers', 'both', 'mutual'])
with row0[2]:
limit = st.slider('Minimum Movies You Both Have Rated', 1, 100, 20)
row_button = st.columns((6,1,1,6))
submit = row_button[1].button('Submit')
reset = row_button[2].button('Reset')
result = False
if submit:
result = True
if reset:
result = False
if result:
today = date.today()
filename = "{0}_{1}_{2}_{3}".format(str(today), username, ftype, str(limit))
# df_log = pd.read_csv("log.csv")
result_input = sheet.values().get(spreadsheetId=st.secrets['SAMPLE_SPREADSHEET_ID_input'],