Skip to content

Commit

Permalink
On full post update also check media exists.
Browse files Browse the repository at this point in the history
Try to download missing or broken media.
  • Loading branch information
ReneHollander committed Apr 24, 2024
1 parent 5131b76 commit 9967972
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
5 changes: 3 additions & 2 deletions rep0st/db/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,12 @@ def get_posts(self, type: Optional[str] = None) -> Query[Post]:
@transactional()
def get_posts_missing_features(self, type: Optional[Type] = None):
session = self._get_session()
q = session.query(Post)
q = session.query(Post).join(Post.features, isouter=True)
if type:
q = q.filter(Post.type == type)
return q.filter(
and_(Post.status == Status.NOT_INDEXED, Post.deleted == False))
and_(Post.status == Status.NOT_INDEXED, Post.deleted == False,
Feature.id == None))

@transactional()
def post_count(self) -> int:
Expand Down
4 changes: 2 additions & 2 deletions rep0st/service/download_media_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from injector import Binder, Module, inject, singleton
from prometheus_client import Counter

from rep0st.db.post import Post, Type
from rep0st.db.post import Post, Status, Type
from rep0st.pr0gramm.api import APIException, Pr0grammAPI, Pr0grammAPIModule
from rep0st.service.media_service import _MediaDirectory, _MediaFlagModule

Expand Down Expand Up @@ -44,7 +44,7 @@ def download_media(self, post: Post):

def _download_media(path: str, dl_fn, dir_prefix: str = ''):
media_file = self.media_dir / dir_prefix / path
if media_file.is_file():
if media_file.is_file() and post.status != Status.MEDIA_BROKEN:
log.debug(
f'Media for post {post.id} found at location {media_file.absolute()}, skipping download'
)
Expand Down
8 changes: 3 additions & 5 deletions rep0st/service/post_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,19 @@ def _process_batch(self, batch_start_id: int, batch_end_id: int,
to_save.append(post_from_db)
continue
# Post is in both DB and API. Potentially update it.
dirty = False
if post_from_db.deleted:
log.debug(
f'Unmarking post as deleted since the API contains it: {post_from_db}'
)
post_from_db.deleted = False
dirty = True
if post_from_db.flags != post_from_api.flags:
log.debug(
f'Updating flags of post since they changed: {post_from_db}. post_from_db.flags={post_from_db.flags}, post_from_api.flags={post_from_api.flags}'
)
post_from_db.flags = post_from_api.flags
dirty = True
if dirty:
to_save.append(post_from_db)
# Download media if not exists or broken.
self._download_media(post_from_db)
to_save.append(post_from_db)
# TODO(https://github.com/ReneHollander/rep0st/issues/42): Sync updates posts to elasticsearch index.
self.post_repository.persist_all(to_save)

Expand Down

0 comments on commit 9967972

Please sign in to comment.