Esempio n. 1
0
    def _ng_changed(self, e: CollectionChangedEventArgs):
        if self._thread is not None:
            self._responses = []
            self._links = []
            self._replies = {}
            self._ids = {}

            for r in self._thread.responses:
                self._filter_response(r)

            self.on_property_changed.invoke(
                PropertyChangedEventArgs(self, "responses"))

        self.on_property_changed.invoke(PropertyChangedEventArgs(self, "ng"))
Esempio n. 2
0
    def save(self, board: str, key: str, bookmark: int, retrieved_reses: int):
        while self._max_history <= len(self._dict):
            self._dict.popitem()

        self._dict[board + key] = ThreadHistory(bookmark, retrieved_reses)

        self.on_property_changed.invoke(PropertyChangedEventArgs(self, "self"))
Esempio n. 3
0
    def set_image(self, url: str):
        if CACHE_IMAGE:
            file_name = re.sub(r'https?://|/', "", url)

            if image_cache.contains(file_name):
                self.image = image_cache.path + "/" + file_name
            else:
                result = download_image(url)

                if isinstance(result, HTTPError) or isinstance(
                        result, URLError):
                    self.image = result
                else:
                    image_cache.store(file_name, result)
                    self.image = image_cache.path + "/" + file_name
        else:
            result = download_image(url)

            if isinstance(result, HTTPError) or isinstance(result, URLError):
                self.image = result
            else:
                f = tempfile.NamedTemporaryFile()
                f.write(result)
                self.image = f.name
                f.close()

        self.on_property_changed.invoke(PropertyChangedEventArgs(
            self, "image"))
Esempio n. 4
0
    def set_board(self, server: str, board: str):
        if CACHE_BOARD:
            self.save_board()

            if board_cache.contains(board):
                s = board_cache.get(board)
                self.board = Board.deserialize(s)
                self.board.update()
                self.on_property_changed.invoke(
                    PropertyChangedEventArgs(self, "board"))
                return

        self.board = Board(server, board)
        self.board.update()
        self.on_property_changed.invoke(PropertyChangedEventArgs(
            self, "board"))
Esempio n. 5
0
    def set_thread(self, server: str, board: str, key: str):
        if CACHE_THREAD:
            self.save_thread()

            if thread_cache.contains(board + key):
                s = thread_cache.get(board + key)
                self.thread = Thread.deserialize(s)
                self.thread.update()
                self.on_property_changed.invoke(
                    PropertyChangedEventArgs(self, "thread"))
                return

        self.thread = Thread(server, board, key)
        self.thread.init()
        self.on_property_changed.invoke(
            PropertyChangedEventArgs(self, "thread"))
Esempio n. 6
0
    def update(self):
        s = get_board(self.server, self.board)
        parser = BoardParser(s)

        new_threads = []

        for i, t in enumerate(parser.threads(), 1):
            # If thread is new arrival
            if t["key"] not in [x.key for x in self.threads]:
                now = int(time.time())
                key = int(t["key"])

                # If the time since thread is created is less than NEW_THREAD_INTERVAL minutes from now
                if now - (NEW_THREAD_INTERVAL * 60) < key:
                    new_threads.append(
                        ThreadHeader(t["key"], i, t["title"], t["count"],
                                     True))
                    continue

            new_threads.append(
                ThreadHeader(t["key"], i, t["title"], t["count"], False))

        self.threads = new_threads

        self.on_property_changed.invoke(
            PropertyChangedEventArgs(self, "threads"))
Esempio n. 7
0
    def update(self):
        html = get_responses_after(self.server, self.board, self.key,
                                   len(self.responses))
        parser = ThreadParserH(html)

        self.is_pastlog = parser.is_pastlog()
        self.on_property_changed.invoke(
            PropertyChangedEventArgs(self, "is_pastlog"))

        new_responses = []
        for r in parser.responses():
            new_responses.append(
                Response(r["number"], r["name"], r["mail"], r["date"], r["id"],
                         r["message"]))

        last_count = len(self.responses)

        # There are no new posts
        if len(new_responses) == 1:
            return

        self.responses.extend(new_responses[1:])

        self.on_collection_changed.invoke(
            CollectionChangedEventArgs(self, "responses",
                                       CollectionChangedEventKind.EXTEND,
                                       self.responses[last_count:]))
Esempio n. 8
0
    def _app_context_changed(self, e: PropertyChangedEventArgs):
        if e.property_name == "thread":
            if self._thread is not None:
                self._thread.on_property_changed.remove(
                    self._thread_property_changed)
                self._thread.on_collection_changed.remove(
                    self._thread_collection_changed)

            self._thread = self._app_context.thread
            self._thread.on_property_changed.add(self._thread_property_changed)
            self._thread.on_collection_changed.add(
                self._thread_collection_changed)

            self._responses = []
            self._links = []
            self._replies = {}
            self._ids = {}

            for r in self._thread.responses:
                self._filter_response(r)

            self.on_property_changed.invoke(
                PropertyChangedEventArgs(self, "server"))
            self.on_property_changed.invoke(
                PropertyChangedEventArgs(self, "board"))
            self.on_property_changed.invoke(
                PropertyChangedEventArgs(self, "key"))
            self.on_property_changed.invoke(
                PropertyChangedEventArgs(self, "title"))
            self.on_property_changed.invoke(
                PropertyChangedEventArgs(self, "is_pastlog"))
            self.on_property_changed.invoke(
                PropertyChangedEventArgs(self, "responses"))
            self.on_property_changed.invoke(
                PropertyChangedEventArgs(self, "links"))
Esempio n. 9
0
    def _app_context_changed(self, e: PropertyChangedEventArgs):
        if e.property_name == "bbsmenu":
            if self._bbsmenu is not None:
                self._bbsmenu.on_property_changed.remove(self._bbsmenu_changed)

            self._bbsmenu = self._app_context.bbsmenu
            self._bbsmenu.on_property_changed.add(self._bbsmenu_changed)

            self.on_property_changed.invoke(
                PropertyChangedEventArgs(self, "categories"))
Esempio n. 10
0
    def update(self):
        html = get_bbsmenu()
        parser = BbsmenuParser(html)
        self.categories = []

        for c in parser.categories():
            boards = []
            for b in c["boards"]:
                boards.append(BoardHeader(b["server"], b["board"], b["name"]))
                self.dns[b["board"]] = b["name"]

            self.categories.append(Category(c["name"], boards))

        self.on_property_changed.invoke(PropertyChangedEventArgs(self, "categories"))
Esempio n. 11
0
    def init(self):
        html = get_responses_after(self.server, self.board, self.key,
                                   len(self.responses))
        parser = ThreadParserH(html)

        self.is_pastlog = parser.is_pastlog()

        if self.title is None:
            self.title = parser.title()

        for r in parser.responses():
            self.responses.append(
                Response(r["number"], r["name"], r["mail"], r["date"], r["id"],
                         r["message"]))

        self.on_property_changed.invoke(
            PropertyChangedEventArgs(self, "is_pastlog"))
        self.on_property_changed.invoke(PropertyChangedEventArgs(
            self, "title"))

        self.on_collection_changed.invoke(
            CollectionChangedEventArgs(self, "responses",
                                       CollectionChangedEventKind.EXTEND,
                                       self.responses[0:]))
Esempio n. 12
0
    def sort_threads(self, key: str, reverse=False):
        if key == "number":
            self.threads.sort(key=lambda x: x.number, reverse=reverse)
        elif key == "title":
            self.threads.sort(key=lambda x: x.title, reverse=reverse)
        elif key == "count":
            self.threads.sort(key=lambda x: x.count, reverse=reverse)
        elif key == "unread":
            self.threads.sort(key=lambda x: x.count - x.bookmark
                              if x.bookmark != 0 else -1,
                              reverse=True)
        elif key == "speed":
            self.threads.sort(key=lambda x: x.speed, reverse=reverse)

        self.on_property_changed.invoke(
            PropertyChangedEventArgs(self, "threads"))
Esempio n. 13
0
    def _update_threads(self):
        if self._board is None:
            return

        self._threads = []

        for t in self._app_context.ng.filter_threads(self._board.threads,
                                                     self._board.board):
            history = self._app_context.history.get(self._board.board, t.key)

            if history is not None:
                unread = max(t.count,
                             history.retrieved_reses) - history.bookmark
                self._threads.append(
                    ThreadHeaderVM(t.key, t.number, t.title,
                                   max(t.count, history.retrieved_reses),
                                   t.is_new, t.speed, unread))
            else:
                self._threads.append(
                    ThreadHeaderVM(t.key, t.number, t.title, t.count, t.is_new,
                                   t.speed, None))

        if self._sort_by == "number":
            self._threads.sort(key=lambda x: x.number,
                               reverse=self._reverse_sort)
        elif self._sort_by == "title":
            self._threads.sort(key=lambda x: x.title,
                               reverse=self._reverse_sort)
        elif self._sort_by == "count":
            self._threads.sort(key=lambda x: x.count,
                               reverse=self._reverse_sort)
        elif self._sort_by == "speed":
            self._threads.sort(key=lambda x: x.speed,
                               reverse=self._reverse_sort)

        if self._active_sort:
            self._threads.sort(key=self._active_sort_key, reverse=True)

        if self._search_word is not None:
            self._threads.sort(
                key=lambda x: (self._search_word not in x.title))

        self.on_property_changed.invoke(
            PropertyChangedEventArgs(self, "threads"))
Esempio n. 14
0
 def favorite_changed(self, e: PropertyChangedEventArgs):
     self.on_property_changed.invoke(
         PropertyChangedEventArgs(self, "is_favorite"))
Esempio n. 15
0
 def _context_changed(self, e: PropertyChangedEventArgs):
     if e.property_name == "image":
         self.on_property_changed.invoke(PropertyChangedEventArgs(self, "image"))
Esempio n. 16
0
 def select_category(self, idx: int):
     if self._bbsmenu is not None and idx < len(self._bbsmenu.categories)\
             and idx >= 0:
         self.selected_category = self._bbsmenu.categories[idx]
         self.on_property_changed.invoke(
             PropertyChangedEventArgs(self, "selected_category"))
Esempio n. 17
0
 def set_bbsmenu(self):
     self.bbsmenu = Bbsmenu()
     self.bbsmenu.update()
     self.on_property_changed.invoke(
         PropertyChangedEventArgs(self, "bbsmenu"))
Esempio n. 18
0
 def _history_changed(self, e: PropertyChangedEventArgs):
     self.on_property_changed.invoke(
         PropertyChangedEventArgs(self, "history"))
Esempio n. 19
0
 def _bbsmenu_changed(self, e: PropertyChangedEventArgs):
     if e.property_name == "categories":
         self.on_property_changed.invoke(
             PropertyChangedEventArgs(self, "categories"))
Esempio n. 20
0
 def is_pastlog(self, value: bool):
     self._is_pastlog = value
     self.on_property_changed.invoke(
         PropertyChangedEventArgs(self, "is_pastlog"))
Esempio n. 21
0
 def _context_changed(self, e: PropertyChangedEventArgs):
     self.on_property_changed.invoke(PropertyChangedEventArgs(self, "list"))
Esempio n. 22
0
 def _thread_property_changed(self, e: PropertyChangedEventArgs):
     if e.property_name == "bookmark":
         self.on_property_changed.invoke(
             PropertyChangedEventArgs(self, "threads"))
Esempio n. 23
0
 def remove(self, item: Union[FavoriteThread, FavoriteBoard]):
     self.list.remove(item)
     self.on_property_changed.invoke(PropertyChangedEventArgs(self, "list"))
Esempio n. 24
0
 def sort_threads_by_word(self, word: str):
     self.threads.sort(key=lambda x: (word not in x.title))
     self.on_property_changed.invoke(
         PropertyChangedEventArgs(self, "threads"))
Esempio n. 25
0
 def _ng_changed(self, e: CollectionChangedEventArgs):
     self.on_property_changed.invoke(
         PropertyChangedEventArgs(self, e.property_name))
Esempio n. 26
0
 def _thread_property_changed(self, e: PropertyChangedEventArgs):
     if e.property_name == "is_pastlog":
         self.on_property_changed.invoke(
             PropertyChangedEventArgs(self, "is_pastlog"))