Last active
January 1, 2019 01:07
-
-
Save curtwagner1984/c091e4ec262289e2c59e911137d1b5c6 to your computer and use it in GitHub Desktop.
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
| import os | |
| import sys | |
| import praw | |
| import time | |
| import pprint | |
| from PySide2 import QtCore | |
| from PySide2.QtCore import Qt, QObject | |
| from PySide2.QtGui import QGuiApplication | |
| from PySide2.QtQml import QQmlApplicationEngine, QQmlError | |
| from praw.models import Submission | |
| from models.submission_model import SubmissionModel, RedditSubmission | |
| def get_reddit(): | |
| reddit = praw.Reddit(client_id='pgiIBpMHArgmeQ', | |
| client_secret='', | |
| user_agent='', | |
| username='', | |
| password='') | |
| return reddit | |
| def fetch(submission_model: SubmissionModel,sub_name): | |
| reddit = get_reddit() | |
| subreddit = reddit.subreddit(sub_name) | |
| submissions = subreddit.new(limit=10) | |
| counter = 0 | |
| for submission in submissions: | |
| is_found = False | |
| counter += 1 | |
| assert isinstance(submission, Submission) | |
| print("#{} Submission Title {} Submission URL: {} ".format(counter, submission.title, submission.url)) | |
| sub = RedditSubmission(submission.name, | |
| submission.title, | |
| submission.url.replace('https', 'http'), | |
| submission.created, | |
| submission.is_video, | |
| submission.ups, | |
| submission.subreddit.display_name | |
| ) | |
| if submission.is_video: | |
| print("Submission : {} is video".format(sub)) | |
| submission_model.add_submission(sub) | |
| def qt_message_handler(mode, context, message): | |
| if mode == QtCore.QtInfoMsg: | |
| mode = 'Info' | |
| elif mode == QtCore.QtWarningMsg: | |
| mode = 'Warning' | |
| elif mode == QtCore.QtCriticalMsg: | |
| mode = 'critical' | |
| elif mode == QtCore.QtFatalMsg: | |
| mode = 'fatal' | |
| else: | |
| mode = 'Debug' | |
| print("%s: %s (%s:%d, %s)" % (mode, message, context.file, context.line, context.file)) | |
| if __name__ == '__main__': | |
| my_submission_model = SubmissionModel() | |
| fetch(my_submission_model,'LandscapePhotography') | |
| my_submission_model.sort_by_upvotes() | |
| os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material" | |
| QtCore.qInstallMessageHandler(qt_message_handler) | |
| app = QGuiApplication(sys.argv) | |
| engine = QQmlApplicationEngine() | |
| engine.setOutputWarningsToStandardError(True) | |
| engine.warnings.connect(handleWarnings) | |
| ctx = engine.rootContext() | |
| ctx.setContextProperty("submissionModel", my_submission_model) | |
| qml_filename = os.path.join(os.path.dirname(__file__), 'QML/main.qml') | |
| if not engine.load(QtCore.QUrl.fromLocalFile(qml_filename)): | |
| sys.exit(-1) | |
| sys.exit(app.exec_()) |
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
| import QtQuick 2.10 | |
| import QtQuick.Controls 2.4 | |
| import QtQuick.Window 2.2 | |
| import QtQuick.Controls.Material 2.3 | |
| import QtQuick.Layouts 1.3 | |
| //import 'D:/Windows/Documents/RedditViewer/QML/TestDelegate.qml' | |
| //import "./SubmissionDelegate.qml" as SubmissionDelegate | |
| //import "./TestDelegate.qml" as TestDelegate | |
| ApplicationWindow { | |
| id: applicationWindow | |
| Material.theme: Material.Dark | |
| visibility: "FullScreen" | |
| title: qsTr("Test QML") | |
| visible: true | |
| width: "josephe" | |
| height: 480 | |
| Component { | |
| id: submissionDelegate | |
| Item { | |
| height: applicationWindow.height | |
| width: applicationWindow.width | |
| BusyIndicator { | |
| anchors.centerIn: parent | |
| running: image.status === Image.Loading | |
| } | |
| Image { | |
| id: image | |
| height: parent.height - 5 | |
| width: parent.width - 5 | |
| source: url | |
| sourceSize.width: image.width | |
| sourceSize.height: image.height | |
| asynchronous: true | |
| fillMode: Image.PreserveAspectFit | |
| onStateChanged: { | |
| imageStatus.text = image.status + " Progress: " + image.progress | |
| } | |
| onProgressChanged: { | |
| imageStatus.text = image.status + " Progress: " + image.progress | |
| } | |
| Text { | |
| id: imageStatus | |
| } | |
| } | |
| Item { | |
| id: headerWrapper | |
| height: applicationWindow.height / 20 | |
| width: applicationWindow.width | |
| Rectangle{ | |
| id:headerBackground | |
| color: "#3FBFBF" | |
| opacity: 0.2 | |
| anchors.fill: parent | |
| } | |
| Text{ | |
| id:submissionTitle | |
| width: parent.width / 2 | |
| text: '/r/' + subreddit +": " + title | |
| font.pixelSize: parent.height - 2 | |
| anchors.left: parent.left | |
| anchors.verticalCenter: parent.verticalCenter | |
| elide: Text.ElideRight | |
| } | |
| Text{ | |
| id:numberOfUpvotes | |
| text: upvotes | |
| font.pixelSize: parent.height - 2 | |
| anchors.right: parent.right | |
| anchors.verticalCenter: parent.verticalCenter | |
| } | |
| } | |
| } | |
| } | |
| Rectangle { | |
| id: background | |
| color: "#b74949" | |
| anchors.fill: parent | |
| ListView { | |
| id:listView | |
| width: background.width | |
| height: background.height | |
| snapMode: ListView.SnapToItem | |
| keyNavigationEnabled: true | |
| // anchors.fill: parent | |
| model: submissionModel | |
| delegate: submissionDelegate | |
| } | |
| } | |
| } |
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
| import datetime | |
| import PySide2 | |
| from PySide2.QtCore import QAbstractListModel, QModelIndex, Qt | |
| # from typing import List | |
| class RedditSubmission: | |
| def __init__(self, name, title, url, time, is_video, upvotes, subreddit): | |
| self.name = name | |
| self.title = title | |
| self.url = url | |
| self.time = time | |
| self.is_video = is_video | |
| self.upvotes = upvotes | |
| self.subreddit = subreddit | |
| def __str__(self): | |
| time_string = datetime.datetime.fromtimestamp(self.time).strftime('%c') | |
| return "Name:{} Title: {}, Created Time: {}, is_video: {}, # of upvotes: {}".format(self.name, self.title, | |
| time_string, | |
| self.is_video, | |
| self.upvotes) | |
| class SubmissionModel(QAbstractListModel): | |
| NameRole = Qt.UserRole + 1 | |
| TitleRole = Qt.UserRole + 2 | |
| UrlRole = Qt.UserRole + 3 | |
| TimeRole = Qt.UserRole + 4 | |
| IsVideoRole = Qt.UserRole + 5 | |
| UpvotesRole = Qt.UserRole + 6 | |
| SubRedditRole = Qt.UserRole + 7 | |
| _roles = {NameRole: b"name", | |
| TitleRole: b"title", | |
| UrlRole: b"url", | |
| TimeRole: b"time", | |
| IsVideoRole: b"is_video", | |
| UpvotesRole: b"upvotes", | |
| SubRedditRole: b"subreddit", | |
| } | |
| def __init__(self, parent=None): | |
| super(SubmissionModel, self).__init__(parent) | |
| self._submissions = [] | |
| def sort_by_upvotes(self): | |
| self._submissions.sort(key=lambda x: x.upvotes, reverse=True) | |
| def rowCount(self, parent=QModelIndex()): | |
| return len(self._submissions) | |
| def add_submission(self, submission: RedditSubmission): | |
| self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount()) | |
| self._submissions.append(submission) | |
| print("Added submission:{}, to list".format(submission)) | |
| self.endInsertRows() | |
| def roleNames(self): | |
| return self._roles | |
| def data(self, index: PySide2.QtCore.QModelIndex, role: int = ...): | |
| try: | |
| submission = self._submissions[index.row()] | |
| assert isinstance(submission, RedditSubmission) | |
| except IndexError: | |
| return None | |
| if role == self.NameRole: | |
| return submission.name | |
| if role == self.TitleRole: | |
| return submission.title | |
| if role == self.UrlRole: | |
| return submission.url | |
| if role == self.TimeRole: | |
| return submission.time | |
| if role == self.IsVideoRole: | |
| return submission.is_video | |
| if role == self.UpvotesRole: | |
| return submission.upvotes | |
| if role == self.SubRedditRole: | |
| return submission.subreddit | |
| return None |
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
| import QtQuick 2.10 | |
| import QtQuick.Controls 2.4 | |
| import QtQuick.Window 2.2 | |
| import QtQuick.Controls.Material 2.3 | |
| import QtQuick.Layouts 1.3 | |
| Item { | |
| Text { | |
| id: submissionTitle | |
| text: title | |
| anchors.verticalCenter: parent.verticalCenter | |
| height: parent.height / 20 | |
| } | |
| BusyIndicator { | |
| anchors.centerIn: parent | |
| running: image.status === Image.Loading | |
| } | |
| Image { | |
| id: image | |
| height: parent.height - 5 | |
| width: parent.width - 5 | |
| source: link | |
| asynchronous: true | |
| fillMode: Image.PreserveAspectFit | |
| onStateChanged: { | |
| imageStatus.text = image.status + " Progress: " + image.progress | |
| } | |
| onProgressChanged: { | |
| imageStatus.text = image.status + " Progress: " + image.progress | |
| } | |
| Text { | |
| id: imageStatus | |
| } | |
| } | |
| } |
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
| import QtQuick 2.10 | |
| Rectangle { | |
| height: 100 | |
| width: 100 | |
| Text { | |
| id: testText | |
| text: qsTr("This is a test text") | |
| } | |
| } |
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
| import QtQuick 2.10 | |
| import QtQuick.Controls 2.4 | |
| import QtQuick.Window 2.2 | |
| import QtQuick.Controls.Material 2.3 | |
| import QtQuick.Layouts 1.3 | |
| //import 'D:/Windows/Documents/RedditViewer/QML/TestDelegate.qml' | |
| //import "./SubmissionDelegate.qml" as SubmissionDelegate | |
| //import "./TestDelegate.qml" as TestDelegate | |
| ApplicationWindow { | |
| id: applicationWindow | |
| Material.theme: Material.Dark | |
| title: qsTr("Test QML") | |
| visible: true | |
| width: 640 | |
| height: 480 | |
| ListView { | |
| id: listView | |
| width: applicationWindow.width | |
| height: applicationWindow.height | |
| snapMode: ListView.SnapToItem | |
| keyNavigationEnabled: true | |
| // anchors.fill: parent | |
| model: submissionModel | |
| delegate: Item { | |
| Text { | |
| text: name | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment