Skip to content

Commit

Permalink
Merge pull request #51 from mBaratta96/develop
Browse files Browse the repository at this point in the history
switched to shelve
  • Loading branch information
mBaratta96 authored Jul 1, 2023
2 parents 831f66e + c62a3da commit 6548663
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
27 changes: 19 additions & 8 deletions letterboxd_stats/web_scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from letterboxd_stats import cli
import requests
from lxml import html
import pickledb
import shelve

URL = "https://letterboxd.com"
LOGIN_PAGE = URL + "/user/login.do"
Expand All @@ -24,7 +24,6 @@
}

cache_path = os.path.expanduser(os.path.join(config["root_folder"], "static", "cache.db"))
tmdb_id_cache = pickledb.load(cache_path, auto_dump=True)


class Downloader:
Expand Down Expand Up @@ -95,26 +94,38 @@ def create_movie_url(title: str, operation: str):
return URL + OPERATIONS_URLS[operation](title)


def get_tmdb_id(link: str, is_diary: bool):
if tmdb_id_cache.exists(link):
return tmdb_id_cache.get(link)
def _get_tmdb_id_from_web(link: str, is_diary: bool):
res = requests.get(link)
movie_page = html.fromstring(res.text)
if is_diary:
title_link = movie_page.xpath("//span[@class='film-title-wrapper']/a")
if len(title_link) == 0:
return None
raise ValueError("No movie link found.")
movie_link = title_link[0]
movie_url = URL + movie_link.get("href")
movie_page = html.fromstring(requests.get(movie_url).text)
tmdb_link = movie_page.xpath("//a[@data-track-action='TMDb']")
if len(tmdb_link) == 0:
return None
raise ValueError("No Movie link found")
id = tmdb_link[0].get("href").split("/")[-2]
tmdb_id_cache.set(link, id)
return int(id)


def get_tmdb_id(link: str, is_diary: bool):
tmdb_id_cache = shelve.open(cache_path, writeback=False, protocol=5)
if link in tmdb_id_cache:
id = tmdb_id_cache[link]
else:
try:
id = _get_tmdb_id_from_web(link, is_diary)
tmdb_id_cache[link] = id
except ValueError as e:
print(e)
id = None
tmdb_id_cache.close()
return id


def select_optional_operation():
return cli.select_value(["Exit"] + list(MOVIE_OPERATIONS.keys()), "Select operation:")

Expand Down
25 changes: 12 additions & 13 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,26 @@ build-backend = "setuptools.build_meta"

[project]
name = "letterboxd_stats"
version = "0.2.4"
version = "0.2.5"
authors = [{ name = "mBaratta96" }]
description = "Get information about your Letterboxd activity."
readme = "README.md"
license = { file = "LICENSE" }
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]
dependencies = [
"ascii_magic~=2.3.0",
"inquirerpy~=0.3.4",
"lxml~=4.9.0",
"pandas~=1.5.1",
"pickleDB~=0.9.2",
"platformdirs~=3.0.0",
"rich~=13.3.5",
"tmdbv3api~=1.7.7",
"tomli~=2.0.1",
"ascii_magic~=2.3.0",
"inquirerpy~=0.3.4",
"lxml~=4.9.0",
"pandas~=1.5.1",
"platformdirs~=3.0.0",
"rich~=13.3.5",
"tmdbv3api~=1.7.7",
"tomli~=2.0.1",
]

[project.urls]
Expand Down

0 comments on commit 6548663

Please sign in to comment.