Skip to content

Commit

Permalink
Upgrade to latest tibia.py version, now uses json for data storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Galarzaa90 committed Sep 11, 2023
1 parent c6225a8 commit ebe618a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ jobs:
- name: Test
run: |
python setup.py test
python -m unittest discover
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# User files
config.json
config.yml
*.data
data/*

# IDEs
.idea/
Expand Down Expand Up @@ -110,4 +110,4 @@ venv.bak/
/site

# mypy
.mypy_cache/
.mypy_cache/
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Changelog
## Version 3.0.0 (Unreleased)
- **Breaking Change:** Dropped support for **Python 3.7** and lower.
- Guild data is now stored in JSON format.

## Version 2.0.0 (2020-02-22)
- **Breaking Change:** Dropped support for **Python 3.5**.
- **Breaking Change:** Guild data is now read from `data` folder in the working directory.
Expand Down Expand Up @@ -30,4 +34,4 @@ Initial release
- Announces when members are deleted.
- Announces when members get a name change.
- Announces when members get their title changed.
- Configurable via JSON file.
- Configurable via JSON file.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ FROM python:3.11-slim

WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt

COPY guildwatcher.py .

RUN pip install -r requirements.txt

CMD ["python", "guildwatcher.py"]
71 changes: 37 additions & 34 deletions guildwatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@

import requests
import tibiapy
from tibiapy.models import Guild
import yaml
from tibiapy.parsers import CharacterParser, GuildParser
from tibiapy.urls import get_character_url, get_guild_url

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
Expand Down Expand Up @@ -146,15 +149,15 @@ def load_config():
exit()


def save_data(file, data):
def save_data(file, data: Guild):
"""
Saves a guild's data to a file.
:param file: The file's path to save to
:param data: The guild's data.
"""
os.makedirs("data", exist_ok=True)
with open(os.path.join("data", file), "wb") as f:
pickle.dump(data, f, protocol=pickle.HIGHEST_PROTOCOL)
with open(os.path.join("data", file), "w", encoding="utf-8") as f:
f.write(data.model_dump_json())


def load_data(file):
Expand All @@ -165,8 +168,8 @@ def load_data(file):
:rtype: tibiapy.Guild
"""
try:
with open(os.path.join("data", file), "rb") as f:
return pickle.load(f)
with open(os.path.join("data", file), "r", encoding="utf-8") as f:
return Guild.model_validate_json(f.read())
except ValueError:
return None
except FileNotFoundError:
Expand All @@ -184,7 +187,7 @@ def get_character(name, tries=5): # pragma: no cover
:rtype: tibiapy.Character
"""
try:
url = tibiapy.Character.get_url(name)
url = get_character_url(name)
except UnicodeEncodeError:
return None

Expand All @@ -198,7 +201,7 @@ def get_character(name, tries=5): # pragma: no cover
else:
tries -= 1
return get_character(name, tries)
char = tibiapy.Character.from_content(content)
char = CharacterParser.from_content(content)
return char


Expand All @@ -213,7 +216,7 @@ def get_guild(name, tries=5): # pragma: no cover
:rtype: tibiapy.Guild
"""
try:
r = requests.get(tibiapy.Guild.get_url(name))
r = requests.get(get_guild_url (name))
content = r.text
except requests.RequestException:
if tries == 0:
Expand All @@ -222,7 +225,7 @@ def get_guild(name, tries=5): # pragma: no cover
tries -= 1
return get_guild(name, tries)

guild = tibiapy.Guild.from_content(content)
guild = GuildParser.from_content(content)

return guild

Expand Down Expand Up @@ -366,10 +369,10 @@ def compare_guild_invites(after, before, changes, joined):
accepted = True
break
if not accepted:
log.info("Invite rejected or removed: " + removed_invite.name)
log.info(f"Invite rejected or removed: {removed_invite.name}")
changes.append(Change(ChangeType.INVITE_REMOVED, removed_invite))
changes += [Change(ChangeType.NEW_INVITE, i) for i in new_invites]
if len(new_invites) > 0:
if new_invites:
log.info("New invites found: " + ",".join(m.name for m in new_invites))


Expand All @@ -382,14 +385,14 @@ def get_vocation_emoji(vocation):
:rtype: str
"""
return {
tibiapy.Vocation.DRUID: "❄️",
tibiapy.Vocation.ELDER_DRUID: "❄️",
tibiapy.Vocation.KNIGHT: "🛡",
tibiapy.Vocation.ELITE_KNIGHT: "🛡",
tibiapy.Vocation.SORCERER: "🔥",
tibiapy.Vocation.MASTER_SORCERER: "🔥",
tibiapy.Vocation.PALADIN: "🏹",
tibiapy.Vocation.ROYAL_PALADIN: "🏹",
tibiapy.enums.Vocation.DRUID: "❄️",
tibiapy.enums.Vocation.ELDER_DRUID: "❄️",
tibiapy.enums.Vocation.KNIGHT: "🛡",
tibiapy.enums.Vocation.ELITE_KNIGHT: "🛡",
tibiapy.enums.Vocation.SORCERER: "🔥",
tibiapy.enums.Vocation.MASTER_SORCERER: "🔥",
tibiapy.enums.Vocation.PALADIN: "🏹",
tibiapy.enums.Vocation.ROYAL_PALADIN: "🏹",
}.get(vocation, "")


Expand All @@ -401,15 +404,15 @@ def get_vocation_abbreviation(vocation):
:return: The emoji that represents the vocation.
:rtype: str"""
return {
tibiapy.Vocation.DRUID: "D",
tibiapy.Vocation.ELDER_DRUID: "ED",
tibiapy.Vocation.KNIGHT: "K",
tibiapy.Vocation.ELITE_KNIGHT: "EK",
tibiapy.Vocation.SORCERER: "S",
tibiapy.Vocation.MASTER_SORCERER: "MS",
tibiapy.Vocation.PALADIN: "P",
tibiapy.Vocation.ROYAL_PALADIN: "RP",
tibiapy.Vocation.NONE: "N",
tibiapy.enums.Vocation.DRUID: "D",
tibiapy.enums.Vocation.ELDER_DRUID: "ED",
tibiapy.enums.Vocation.KNIGHT: "K",
tibiapy.enums.Vocation.ELITE_KNIGHT: "EK",
tibiapy.enums.Vocation.SORCERER: "S",
tibiapy.enums.Vocation.MASTER_SORCERER: "MS",
tibiapy.enums.Vocation.PALADIN: "P",
tibiapy.enums.Vocation.ROYAL_PALADIN: "RP",
tibiapy.enums.Vocation.NONE: "N",
}.get(vocation, "")


Expand Down Expand Up @@ -572,22 +575,22 @@ def scan_guilds():
log.error("Guild is missing name.")
time.sleep(5)
continue
guild_file = name+".data"
guild_file = f"{name}.json"
guild_data = load_data(guild_file)
if guild_data is None:
log.info(name + " - No previous data found. Saving current data.")
log.info(f"{name} - No previous data found. Saving current data.")
guild_data = get_guild(name)
if guild_data is None:
log.error(name + " - Error: Guild doesn't exist")
log.error(f"{name} - Error: Guild doesn't exist")
continue
save_data(guild_file, guild_data)
time.sleep(5)
continue

log.info(name + " - Scanning guild...")
log.info(f"{name} - Scanning guild...")
new_guild_data = get_guild(name)
if new_guild_data is None:
log.error(name + " - Error: Guild doesn't exist")
log.error(f"{name} - Error: Guild doesn't exist")
continue
save_data(guild_file, new_guild_data)
# Looping through members
Expand All @@ -599,7 +602,7 @@ def scan_guilds():
changes = compare_guild(guild_data, new_guild_data)
embeds = build_embeds(changes)
publish_changes(cfg_guild.webhook_url, embeds, guild_data.name, new_guild_data.logo_url, member_count)
log.info(name + " - Scanning done")
log.info(f"{name} - Scanning done")
time.sleep(2)
time.sleep(cfg.interval)

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
requests
tibia.py
tibia.py>=6
PyYAML

0 comments on commit ebe618a

Please sign in to comment.