Skip to content

Commit

Permalink
load more results & path fix
Browse files Browse the repository at this point in the history
  • Loading branch information
LabAsim committed Mar 7, 2023
1 parent ce378a3 commit c6df318
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
40 changes: 34 additions & 6 deletions App.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class App:
def __init__(self, root, to_bypass, debug):
# Toplevel windows
# The names are PascalCase to be identical to __class__.__name__ of each Toplevel class.

self.topleveldonate = None
self.toplevelabouttpp = None
self.toplevelsocial = None
Expand All @@ -57,6 +56,7 @@ def __init__(self, root, to_bypass, debug):
self.autosave_db = True
self.loading_tk = None
self.dir_path = self.find_current_dir_path()
self.dir_path_of_main = self.find_the_path_of_main()
self.transparency = None
self.help_menu = None
self.database_menu = None
Expand Down Expand Up @@ -88,7 +88,9 @@ def __init__(self, root, to_bypass, debug):
self.searchbox = None
self.search_button = None
self.searchtoplevel = None
self.term = None # The keyword that the user provided
self.search_photo = None
self.search_labelframe = None
self.seach_keyword = None # The keyword that the user provided
self.search_counter = 1
self.create_search_ui()
self.top_parent_label = ttk.Label(self.root)
Expand Down Expand Up @@ -303,7 +305,8 @@ def create_search_ui(self):
width=10, border=0)
self.searchbox.pack(side="left")
self.searchbox.bind("<Return>", self.search_handler)
self.search_photo = Image.open(os.path.join(self.dir_path, "source\\multimedia\\images\\misc\\search.png"))
self.search_photo = Image.open(os.path.join(self.dir_path_of_main,
"source\\multimedia\\images\\misc\\search.png"))
self.search_photo = self.search_photo.resize((35, 35), Image.ANTIALIAS)
self.search_photo = ImageTk.PhotoImage(image=self.search_photo, master=self.search_label)
self.search_button = tk.Button(self.search_label, image=self.search_photo,
Expand All @@ -320,16 +323,16 @@ def search_site(self):
"""Picks the user's input from the search box, searches the TPP site and
retrieves the results from the first page"""
self.searchbox.focus_set()
self.term = self.search_text_var.get().strip()
results = SearchTerm(term=self.term, page_number=1, debug=False)
self.seach_keyword = self.search_text_var.get().strip()
results = SearchTerm(term=self.seach_keyword, page_number=1, debug=False)
print(results.list)
self.search_text_var.set("")
self.searchtoplevel = ToplevelSearch(root=self.root, controller=self, results=results.list)

def search_site_load_more(self):
"""Loads more results for the given keyword. The function is called from ToplevelSearch class"""
self.search_counter += 1
results = SearchTerm(term=self.term, page_number=self.search_counter, debug=False)
results = SearchTerm(term=self.seach_keyword, page_number=self.search_counter, debug=False)
# Merge the new results to the old ones and refill the treeview
self.searchtoplevel.fetched_news += results.list
self.searchtoplevel.fill_treeview()
Expand Down Expand Up @@ -465,6 +468,31 @@ def find_current_dir_path(self) -> str | os.PathLike:
print(f'Script: {self.dir_path}')
return self.dir_path

def find_the_path_of_main(self) -> str:
"""
Finds and returns the path of the main.py or the temporary folder (MEIPP) if the program runs as an exe.
:return: The folder path
"""
if getattr(sys, 'frozen', False):
print(getattr(sys, 'frozen', False))
# The temporary path of the file when the app runs as an .exe
self.dir_path_of_main = os.path.dirname(os.path.realpath(__file__))
# If the path until this step contains \\scrape_tpp_gui, get the parent dir, which is a temp dir(MEIPP).
# self.dir_path = os.path.dirname(self.dir_path)
print(f"{self.name_of_class}>Exe (dir_path_of_main):", self.dir_path_of_main)
return self.dir_path_of_main
elif __file__:
self.dir_path = os.path.dirname(__file__) # We need the parent of the parent of this directory
print(f'{self.name_of_class}>Script (self.dir_path): {self.dir_path}')
return self.dir_path

@property
def name_of_class(self):
"""
:return: The name of the class in lower case.
"""
return self.__class__.__name__.lower()

#######################
# Main Menu functions #
#######################
Expand Down
5 changes: 1 addition & 4 deletions source/classes/search.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import requests
from bs4 import BeautifulSoup
from scrape_tpp_gui.trace_error import trace_error
from NewsDataclass import NewsDataclass
from scrape_tpp_gui.source.classes.NewsDataclass import NewsDataclass


class SearchTerm:
Expand Down Expand Up @@ -58,9 +58,6 @@ def scrape_data(self):
for _date in item.find("div", class_="entry-meta"):
date = _date.text.strip()
# print(_date.text)
#if len(date) == 0:


# The date = "" will raise an IndexError in Newsdataclass, but we don't care about the unixtimestamp
# in this occasion. Thus, debug is set to False. It remains True, for the rest of the program which uses
# the Newsdataclass
Expand Down
18 changes: 14 additions & 4 deletions source/classes/searchtoplevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
import tkinter as tk
from tkinter import ttk
import tkinter.font as font

from scrape_tpp_gui.source.classes.database.SubPageReaderDB import SubPageReader
from scrape_tpp_gui.source.classes.database.helper import ToplevelArticleDatabase
from scrape_tpp_gui.source.classes.generictoplevel import GenericToplevel
from scrape_tpp_gui.trace_error import trace_error
from NewsDataclass import NewsDataclass
from scrape_tpp_gui.source.classes.NewsDataclass import NewsDataclass
from scrape_tpp_gui.helper_functions import sortby, center, headers


Expand All @@ -17,6 +16,7 @@ class ToplevelSearch(GenericToplevel):

def __init__(self, controller, root: tk.Tk, results: list[NewsDataclass]):
super().__init__(controller=controller, root=root)
self.toplevel.withdraw()
self.toplevel: tk.Toplevel = self.toplevel
self.controller = controller
self.root = root
Expand All @@ -30,13 +30,16 @@ def __init__(self, controller, root: tk.Tk, results: list[NewsDataclass]):
self.font = font.Font(size=14)
self.big_frame = None
self.tree = None
self.bar_menu = None
self.main_menu = None
self.right_click_menu = None
self.create_menu()
self.create_ui()
self.setup_treeview()
self.fill_treeview()
self.create_binds()
center(self.toplevel, self.root)
self.toplevel.deiconify()
self.toplevel.lift()

def create_menu(self):
Expand All @@ -48,10 +51,13 @@ def create_menu(self):
self.main_menu = tk.Menu(self.bar_menu, font='Arial 10', tearoff=0, background='black', fg='white')
self.bar_menu.add_cascade(label='Menu', menu=self.main_menu, background='black')
self.main_menu.add_command(label='Load more results', command=self.controller.search_site_load_more)
# self.main_menu.add_command(label='root', command=lambda: center(self.toplevel, self.root))
# self.main_menu.add_command(label='screen', command=lambda: center(self.toplevel))
# Right click menu only for the treeview
self.right_click_menu = tk.Menu(font='Arial 10', tearoff=0)
# Lambda here is needed because there is no event to be passed. If no lambda is used, an error will be raised
self.right_click_menu.add_command(label='Show article', command=lambda: self.show_main_article(event=None))
self.right_click_menu.add_command(label='Load more results', command=self.controller.search_site_load_more)

def create_ui(self):
"""Constructs the user interface"""
Expand Down Expand Up @@ -108,9 +114,12 @@ def fill_treeview(self):
# Fix the lengths
self.tree.column(column='Title', minwidth=100, width=int(max(titles_length)), stretch=True)
self.tree.column(column='Date', minwidth=150, width=int(max(dates_length)), stretch=False)
# Adjust the x axis after the scraped data is loaded in the treeview
self.x = int(max(titles_length)) + int(max(dates_length)) + 150
# Adjust the x-axis after the scraped data is loaded in the treeview
self.x = int(max(titles_length)) + int(max(dates_length))
self.toplevel.withdraw()
self.toplevel.geometry(f"{self.x}x{self.y}")
center(self.toplevel)
self.toplevel.deiconify()
print(f"ToplevelSearch>fill_treeview()>news inserted")

def create_binds(self):
Expand Down Expand Up @@ -186,6 +195,7 @@ def toplevel_quit(self):
from scrape_tpp_gui.helper_functions import center, tkinter_theme_calling, parse_arguments
from scrape_tpp_gui.trace_error import trace_error
from search import SearchTerm

center(root)
tkinter_theme_calling(root)
results = SearchTerm(term="κουλης", page_number=1, debug=False)
Expand Down
2 changes: 1 addition & 1 deletion source/version/version.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "21-12-2022"
"version": "07-03-2023"
}

0 comments on commit c6df318

Please sign in to comment.