-
Notifications
You must be signed in to change notification settings - Fork 45
fix: TimeDelta calculations #270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AxelMurilllo
wants to merge
6
commits into
mathsman5133:master
Choose a base branch
from
AxelMurilllo:fix/TimeDelta
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9e24b1c
fix: TimeDelta calculations
AxelMurilllo f7fa9a7
feat: adding max_level to uninitiated classes
AxelMurilllo f592b7d
fix: parse UpgradeTimeM for troops
AxelMurilllo 7e9407f
Feat: Created scraper to get valid APK url for updating static data
AxelMurilllo ecd21ea
Merge pull request #1 from AxelMurilllo/fix/parsing
AxelMurilllo 5095eca
fix: fixed upgrade_times to not exclude shorter upgrade times
AxelMurilllo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import requests | ||
from bs4 import BeautifulSoup | ||
import re | ||
import time | ||
|
||
APK_MIRROR_BASE = "https://www.apkmirror.com" | ||
COC_PAGE = f"{APK_MIRROR_BASE}/apk/supercell/clash-of-clans/" | ||
|
||
HEADERS = { | ||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" | ||
} | ||
|
||
def get_direct_apk_url(version_suffix="2"): | ||
""" | ||
Skips to the latest APK download page and returns a working intermediate download URL. | ||
This link prompts the browser or urllib to download the actual APK. | ||
""" | ||
session = requests.Session() | ||
session.headers.update(HEADERS) | ||
|
||
print("[*] Fetching main Clash of Clans page...") | ||
resp = session.get(COC_PAGE) | ||
soup = BeautifulSoup(resp.text, "html.parser") | ||
|
||
release_link = soup.select_one("div.appRow a.downloadLink") | ||
if not release_link: | ||
raise Exception("ERROR: No release link found on APKMirror home page") | ||
|
||
release_page_url = APK_MIRROR_BASE + release_link.get("href") | ||
print(f"[+] Latest release page: {release_page_url}") | ||
|
||
# Extract version string from the URL | ||
version_match = re.search(r"clash-of-clans-([\d-]+)-release", release_page_url) | ||
if not version_match: | ||
raise Exception("ERROR: Could not extract version number from release URL") | ||
|
||
version_str = version_match.group(1) | ||
version_segments = version_str.split("-") | ||
version_num = "-".join(version_segments[:3]) | ||
|
||
# Construct the direct variant page | ||
download_page = f"{release_page_url}clash-of-clans-{version_num}-{version_suffix}-android-apk-download/" | ||
print(f"[+] Variant download page: {download_page}") | ||
|
||
variant_page = session.get(download_page) | ||
variant_soup = BeautifulSoup(variant_page.text, "html.parser") | ||
|
||
dl_button = variant_soup.select_one("a.downloadButton") | ||
if not dl_button: | ||
raise Exception("ERROR:Download button not found on variant page") | ||
|
||
intermediate_url = APK_MIRROR_BASE + dl_button.get("href") | ||
print(f"[+] Final download link (intermediate, triggers download): {intermediate_url}") | ||
return intermediate_url | ||
|
||
if __name__ == "__main__": | ||
print(get_direct_apk_url()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -16,6 +16,9 @@ | |||||||
import os | ||||||||
import zipfile | ||||||||
from collections import defaultdict | ||||||||
import requests | ||||||||
from bs4 import BeautifulSoup | ||||||||
from apk_source import get_direct_apk_url | ||||||||
|
||||||||
TARGETS = [ | ||||||||
("logic/buildings.csv", "buildings.csv"), | ||||||||
|
@@ -29,23 +32,59 @@ | |||||||
("localization/texts.csv", "texts_EN.csv"), | ||||||||
] | ||||||||
|
||||||||
APK_URL = "https://d.apkpure.net/b/APK/com.supercell.clashofclans?version=latest" | ||||||||
APK_URL = get_direct_apk_url() | ||||||||
|
||||||||
def get_fingerprint(): | ||||||||
async def download(): | ||||||||
async with aiohttp.request('GET', APK_URL) as fp: | ||||||||
c = await fp.read() | ||||||||
return c | ||||||||
|
||||||||
data = asyncio.run(download()) | ||||||||
|
||||||||
apk_url = get_direct_apk_url() | ||||||||
print(f"[+] Getting download page: {apk_url}") | ||||||||
|
||||||||
# create a session to handle cookies and redirects | ||||||||
session = requests.Session() | ||||||||
session.headers.update({ | ||||||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36", | ||||||||
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", | ||||||||
}) | ||||||||
|
||||||||
# get the download page | ||||||||
resp = session.get(apk_url) | ||||||||
soup = BeautifulSoup(resp.text, 'html.parser') | ||||||||
|
||||||||
# find the direct download link | ||||||||
download_link = soup.select_one('p:-soup-contains("Your download will start") a') | ||||||||
if not download_link: | ||||||||
raise Exception("ERROR: Could not find direct download link on page") | ||||||||
|
||||||||
# get the relative URL and make it absolute | ||||||||
relative_url = download_link.get('href') | ||||||||
direct_url = f"https://www.apkmirror.com{relative_url}" | ||||||||
print(f"[+] Found direct download URL: {direct_url}") | ||||||||
|
||||||||
# download the APK using the direct URL | ||||||||
print("[+] Downloading APK file...") | ||||||||
response = session.get(direct_url, stream=True) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider verifying the HTTP response status (e.g., checking response.status_code) before processing the content to ensure the request succeeded.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||
|
||||||||
if not response.headers.get('content-type', '').startswith('application/'): | ||||||||
raise Exception("ERROR: Response is not an APK file") | ||||||||
|
||||||||
# save the APK file | ||||||||
with open("apk.zip", "wb") as f: | ||||||||
f.write(data) | ||||||||
zf = zipfile.ZipFile("apk.zip") | ||||||||
with zf.open('assets/fingerprint.json') as fp: | ||||||||
fingerprint = json.loads(fp.read())['sha'] | ||||||||
for chunk in response.iter_content(chunk_size=8192): | ||||||||
if chunk: | ||||||||
f.write(chunk) | ||||||||
|
||||||||
# unzip and extract fingerprint.json | ||||||||
try: | ||||||||
with zipfile.ZipFile("apk.zip", "r") as zf: | ||||||||
with zf.open("assets/fingerprint.json") as fp: | ||||||||
fingerprint = json.loads(fp.read())["sha"] | ||||||||
print(f"[+] Successfully extracted fingerprint: {fingerprint}") | ||||||||
except zipfile.BadZipFile: | ||||||||
raise Exception("ERROR: Downloaded file is not a valid APK (ZIP) file") | ||||||||
finally: | ||||||||
# clean up the APK file | ||||||||
if os.path.exists("apk.zip"): | ||||||||
os.remove("apk.zip") | ||||||||
|
||||||||
os.unlink("apk.zip") | ||||||||
return fingerprint | ||||||||
|
||||||||
# Hard-code or fallback | ||||||||
|
@@ -173,22 +212,24 @@ def process_csv(data, file_path, save_name): | |||||||
|
||||||||
base_level = all_levels[0] | ||||||||
base_cols = list(levels_dict[base_level].keys()) | ||||||||
|
||||||||
# Cover edge cases where some troops only have UpgradeTimeM and UpgradeTimeS if it is added | ||||||||
do_not_promote = {"UpgradeTimeH", "UpgradeTimeM", "UpgradeTimeS"} | ||||||||
|
||||||||
for col in base_cols: | ||||||||
# check if col is present in other levels | ||||||||
found_elsewhere = False | ||||||||
for lvl in all_levels[1:]: | ||||||||
if col in levels_dict[lvl]: | ||||||||
found_elsewhere = True | ||||||||
break | ||||||||
if col in do_not_promote: | ||||||||
continue | ||||||||
|
||||||||
found_elsewhere = any(col in levels_dict[lvl] for lvl in all_levels[1:]) | ||||||||
# if not found in other levels => move it up | ||||||||
if not found_elsewhere: | ||||||||
if col not in levels_dict: | ||||||||
# move the single-value column up | ||||||||
final_data[troop_name][col] = levels_dict[base_level][col] | ||||||||
# remove from base_level | ||||||||
del levels_dict[base_level][col] | ||||||||
|
||||||||
# 4) Write final JSON | ||||||||
import json | ||||||||
with open(f"{save_name}.json", "w", encoding="utf-8") as jf: | ||||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding a space after 'ERROR:' in the exception message for consistency.
Copilot uses AI. Check for mistakes.