Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Showcase
  4. A PySide6 inspector for mixed media API responses
Qt 6.12 LTS is out! See what's new in the release blog

A PySide6 inspector for mixed media API responses

Scheduled Pinned Locked Moved Unsolved Showcase
1 Posts 1 Posters 76 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mr-ZhangBo
    wrote last edited by
    #1

    I put together a small Qt Widgets inspector for media API responses. One public post can return photos, multiple video variants, and a separate audio track. The app lets you paste a JSON response and see each media entry in a table, with a separate message for empty results and malformed responses.

    Save the snippet as media_inspector.py. Install PySide6-Essentials and run python media_inspector.py. It opens with a synthetic example. Edit the input and click Inspect response to see how the table changes.

    import json
    import sys
    
    from PySide6.QtWidgets import (
        QApplication,
        QLabel,
        QPlainTextEdit,
        QPushButton,
        QTableWidget,
        QTableWidgetItem,
        QVBoxLayout,
        QWidget,
    )
    
    
    EXAMPLE = {
        "status": 200,
        "data": {"images": [{}], "videos": [{"quality": "720p"}], "audios": []},
    }
    
    
    def media_rows(body):
        response = json.loads(body)
        if not isinstance(response, dict) or response.get("status") != 200:
            raise ValueError("Expected an API response with status 200.")
    
        data = response.get("data")
        if not isinstance(data, dict):
            raise ValueError("The response is missing its data object.")
    
        rows = []
        for field, kind in (("images", "Image"), ("videos", "Video"), ("audios", "Audio")):
            entries = data.get(field, [])
            if not isinstance(entries, list):
                raise ValueError(f"{field} must be a list.")
            for index, entry in enumerate(entries, start=1):
                if not isinstance(entry, dict):
                    raise ValueError(f"{field} entry {index} must be an object.")
                quality = entry.get("quality")
                rows.append((kind, str(index), quality if isinstance(quality, str) else ""))
        return rows
    
    
    class MediaInspector(QWidget):
        def __init__(self):
            super().__init__()
            self.setWindowTitle("Media response inspector")
            self.resize(680, 440)
    
            self.source = QPlainTextEdit(json.dumps(EXAMPLE, indent=2))
            self.status = QLabel()
            self.table = QTableWidget(0, 3)
            self.table.setHorizontalHeaderLabels(["Media type", "Entry", "Quality"])
            self.table.setEditTriggers(QTableWidget.EditTrigger.NoEditTriggers)
            inspect_button = QPushButton("Inspect response")
            inspect_button.clicked.connect(self.inspect)
    
            layout = QVBoxLayout(self)
            for widget in (self.source, inspect_button, self.status, self.table):
                layout.addWidget(widget)
            self.inspect()
    
        def inspect(self):
            self.table.setRowCount(0)
            try:
                rows = media_rows(self.source.toPlainText())
            except json.JSONDecodeError:
                self.status.setText("The input is not valid JSON.")
                return
            except ValueError as error:
                self.status.setText(str(error))
                return
    
            self.table.setRowCount(len(rows))
            for row_index, row in enumerate(rows):
                for column_index, value in enumerate(row):
                    self.table.setItem(row_index, column_index, QTableWidgetItem(value))
            self.status.setText(f"{len(rows)} media entries." if rows else "No media entries.")
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = MediaInspector()
        window.show()
        sys.exit(app.exec())
    

    The desktop sample works on pasted data and makes no network requests. It leaves quality blank when the response omits it, and the table deliberately leaves media URLs out. Clearing the table before parsing means an invalid response cannot leave stale rows on screen. If you wire this into a real client, handle the HTTP response and keep any API credentials on your server.

    I work on EasyDown, the paid API whose response format inspired the sample. The reference explains which media collections each supported platform can return for an individual public post. Use it for media you have permission to process. EasyDown API reference

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt
    • Unsolved