Skip to content

Commit

Permalink
Final code refinements, package EXE, write readme
Browse files Browse the repository at this point in the history
A few final changes to code have been applied. A basic icon has been created. The pip requirements file has been created outlining dependencies. A script to seamlessly convert the program to EXE has been added (requires virtual environment). Add License. Create very detailed readme - including guide images for illustration. All done! Very good project work!
  • Loading branch information
leoz0214 committed Mar 4, 2024
1 parent 953d4e5 commit 1416fc6
Show file tree
Hide file tree
Showing 18 changed files with 279 additions and 33 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
**/__pycache__
**/__pycache__
Include
Lib
Scripts
pyvenv.cfg
release
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2024 Leo Zhang

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
208 changes: 183 additions & 25 deletions README.md

Large diffs are not rendered by default.

Binary file added guide_images/file_input.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added guide_images/file_save_as.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added guide_images/finishers_against_date.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added guide_images/output.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added guide_images/page_save_as.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added guide_images/url_input_empty.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added guide_images/url_input_filled.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icon.ico
Binary file not shown.
55 changes: 55 additions & 0 deletions package_release.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""
Entire release creation - EXE.
Must use virtual environment, ensure Pyinstaller is installed.
"""
import os
import pathlib
import shutil
import subprocess


FOLDER = pathlib.Path(__file__).parent
VENV_SCRIPTS_FOLDER = FOLDER / "Scripts"
RELEASE_FOLDER = FOLDER / "release"
if RELEASE_FOLDER.is_dir():
shutil.rmtree(RELEASE_FOLDER)
RELEASE_FOLDER.mkdir()

CODE_PACKAGE_FOLDER = RELEASE_FOLDER / "code"
CODE_PACKAGE_ZIP = RELEASE_FOLDER / "code"

ICON = FOLDER / "icon.ico"
START_SCRIPT = FOLDER / "src" / "main.py"

ADDITIONAL_DATA = {
FOLDER / "icon.ico": "."
}
OUTPUT_EXE_NAME = RELEASE_FOLDER / "parkrunscraper.exe"


def package_exe() -> None:
"""Packages standalone EXE, ready to run."""
# Turn on virtual environment.
subprocess.run((str(VENV_SCRIPTS_FOLDER / "activate.bat"),))
# Build command parts.
command_parts = [
str(VENV_SCRIPTS_FOLDER / "pyinstaller"),
str(START_SCRIPT), "--noconfirm", "--onefile",
"--windowed", "--icon", str(ICON)]
for add_data_src, add_data_dest in ADDITIONAL_DATA.items():
command_parts.append("--add-data")
command_parts.append(f"{add_data_src};{add_data_dest}")
# Run Pyinstaller.
os.chdir(RELEASE_FOLDER)
subprocess.run(command_parts)
# Renames output EXE.
output_exe = RELEASE_FOLDER / "dist" / f"{START_SCRIPT.stem}.exe"
output_exe.rename(OUTPUT_EXE_NAME)
# Deletes Pyinstaller files and folders.
(RELEASE_FOLDER / "dist").rmdir()
shutil.rmtree(RELEASE_FOLDER / "build")
(RELEASE_FOLDER / f"{START_SCRIPT.stem}.spec").unlink(True)


if __name__ == "__main__":
package_exe()
10 changes: 10 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
selenium # Automated scraping.
beautifulsoup4 # HTML parsing.
lxml # Fast parsing of data.

matplotlib # Graphs
openpyxl # Export to XLSX
python-docx # Export to DOCX

comtypes # Export to PDF on Windows
docx2pdf # Export to PDF on MacOS
6 changes: 4 additions & 2 deletions src/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,10 @@ def process(self) -> None:
options.add_argument(f"user-agent={USER_AGENT}")
# Just to be safe, disguise bot further.
options.add_experimental_option("useAutomationExtension", False)
options.add_experimental_option("excludeSwitches",["enable-automation"])
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_experimental_option(
"excludeSwitches", ["enable-automation"])
options.add_argument(
"--disable-blink-features=AutomationControlled")
url = parse_url(self.url)
with Chrome(options=options) as driver:
if self.cancelled:
Expand Down
11 changes: 10 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Main module of the program - this consists of the GUI and
various input and output screens.
"""
import pathlib
import sys
import tkinter as tk
from ctypes import windll
from tkinter import messagebox
Expand All @@ -12,8 +14,14 @@
import output


windll.shcore.SetProcessDpiAwareness(1) # Enhanced GUI quality.
windll.shcore.SetProcessDpiAwareness(True) # Enhanced GUI quality.
TITLE = "Parkrun Data Scraper"
if hasattr(sys, "_MEIPASS"):
# Executable
ICON_PATH = pathlib.Path(sys._MEIPASS) / "icon.ico"
else:
# Through Python.
ICON_PATH = pathlib.Path(__file__).parent.parent / "icon.ico"


class ParkrunScraper(tk.Tk):
Expand All @@ -23,6 +31,7 @@ def __init__(self) -> None:
super().__init__()
self.title(TITLE)
self.resizable(False, False)
self.iconbitmap(ICON_PATH)

self.notebook = ttk.Notebook(self)
self.automatic_scraper = auto.AutomaticScraper(self.notebook)
Expand Down
4 changes: 2 additions & 2 deletions src/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from matplotlib.ticker import MaxNLocator

import save
from utils import hh_mm_ss_to_seconds, seconds_to_mmss
from utils import hhmmss_to_seconds, seconds_to_mmss


@dataclass
Expand Down Expand Up @@ -206,7 +206,7 @@ def parse_source(source: str) -> Data:
finishers = int(get_stat_value(soup, "Finishers:").replace(",", ""))
volunteers = int(get_stat_value(soup, "Volunteers:").replace(",", ""))
personal_bests = int(get_stat_value(soup, "PBs:").replace(",", ""))
mean_finish_seconds = hh_mm_ss_to_seconds(
mean_finish_seconds = hhmmss_to_seconds(
get_stat_value(soup, "Average finish time:"))
groups = int(get_stat_value(soup, "Groups:").replace(",", ""))
email = soup.find("a", href=lambda href: href and "mailto" in href).text
Expand Down
2 changes: 1 addition & 1 deletion src/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,4 @@ def save_pdf(data: "output.Data", file_path: str) -> None:
# For MacOS, attempt conversion using docx2pdf.
docx2pdf.convert(temp_file_path, file_path, keep_active=True)
finally:
temp_file_path.unlink(missing_ok=True)
temp_file_path.unlink(missing_ok=True)
2 changes: 1 addition & 1 deletion src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
GREEN = "green"


def hh_mm_ss_to_seconds(time_: str) -> int:
def hhmmss_to_seconds(time_: str) -> int:
"""Converts HH:MM:SS into seconds."""
hours, minutes, seconds = map(int, time_.split(":"))
return hours * 3600 + minutes * 60 + seconds
Expand Down

0 comments on commit 1416fc6

Please sign in to comment.