Skip to content

Commit

Permalink
only upload if gameloot changed, remove seen_last in feed
Browse files Browse the repository at this point in the history
  • Loading branch information
eikowagenknecht committed Feb 21, 2022
1 parent 13c2607 commit ecbd545
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ This project is in ongoing development, not yet usable for the average user and
- [x] Script for data migration in case of updates
- [x] Store all dates in UTC
- [x] Support start and end dates of offers
- [ ] Only upload if hash of gameloot.xml has changed
- [x] Only upload if hash of gameloot.xml has changed
- [ ] Add tests (pytest?)
- [ ] Dynamically generate ATOM feeds split by source and type (e.g. only amazon ingame loot) in addition to the full feed
- [ ] Error handling
Expand Down
10 changes: 3 additions & 7 deletions app/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@

from feedgen.feed import FeedGenerator

from app.configparser import Config

from .common import TIMESTAMP_LONG, TIMESTAMP_READABLE_WITH_HOUR, LootOffer


def generate_feed(offers: list[LootOffer]) -> None:
def generate_feed(offers: list[LootOffer], out_file: Path) -> None:
last_updated = datetime.now(timezone.utc)

# Generate Feed Info
Expand Down Expand Up @@ -54,7 +52,7 @@ def generate_feed(offers: list[LootOffer]) -> None:
title += f": {offer.subtitle}"
title += f" ({offer.type})"
feed_entry.title(title)
feed_entry.updated(offer.seen_last)
feed_entry.updated(offer.seen_first)
# Atom Recommended
# - Author
feed_entry.author(
Expand All @@ -80,7 +78,7 @@ def generate_feed(offers: list[LootOffer]) -> None:
if offer.publisher:
content += f"<li>Publisher: {html.escape(offer.publisher)}</li>"
content += "</ul><p>"
content += f"<small>Source: {html.escape(offer.source)}, Seen first: {offer.seen_first.strftime(TIMESTAMP_LONG)}, Seen last: {offer.seen_last.strftime(TIMESTAMP_LONG)}</small>"
content += f"<small>Source: {html.escape(offer.source)}, Seen first: {offer.seen_first.strftime(TIMESTAMP_LONG)}</small>"
content += "</p>"
feed_entry.content(content, type="xhtml")
# - Link
Expand All @@ -96,7 +94,5 @@ def generate_feed(offers: list[LootOffer]) -> None:

# feed_entry.link(href="http://lernfunk.de/feed")

out_file = Config.data_path() / Path(Config.config()["common"]["FeedFile"])

# Write the ATOM feed to a file
feed_generator.atom_file(filename=str(out_file), pretty=True)
33 changes: 29 additions & 4 deletions lootscraper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import hashlib
import logging
import shutil
import sys
Expand Down Expand Up @@ -128,15 +129,23 @@ def job() -> None:
cfg_generate_feed: bool = Config.config().getboolean("actions", "GenerateFeed") # type: ignore
cfg_upload: bool = Config.config().getboolean("actions", "UploadFtp") # type: ignore

feed_file = Config.data_path() / Path(Config.config()["common"]["FeedFile"])

old_hash = hash_file(feed_file)

if cfg_generate_feed:
generate_feed(all_offers)
generate_feed(all_offers, feed_file)
else:
logging.info("Skipping feed generation")
logging.info("Skipping feed generation, disabled")

new_hash = hash_file(feed_file)

if cfg_upload:
if new_hash == old_hash:
logging.info("Skipping upload, no changes to feed file")
elif cfg_upload:
upload_to_server()
else:
logging.info("Skipping upload")
logging.info("Skipping upload, disabled")


def log_new_offer(offer: LootOffer) -> None:
Expand All @@ -149,6 +158,22 @@ def log_new_offer(offer: LootOffer) -> None:
logging.info(res)


def hash_file(file: Path) -> str:
if not file.exists():
return ""

hash = hashlib.sha256()

with open(file, "rb") as f:
while True:
data = f.read(65536)
if not data:
break
hash.update(data)

return hash.hexdigest()


if __name__ == "__main__":
import signal

Expand Down

0 comments on commit ecbd545

Please sign in to comment.