reddit work - working fetching of posts
This commit is contained in:
parent
332df2ad78
commit
04773acba0
5 changed files with 46 additions and 8 deletions
|
@ -12,7 +12,7 @@
|
|||
|
||||
#define REDDIT_API_GET_PROFILE "https://oauth.reddit.com/api/v1/me"
|
||||
#define REDDIT_API_SUBREDDITS "https://oauth.reddit.com/subreddits/mine/subscriber?limit=%1"
|
||||
#define REDDIT_API_HOT "https://oauth.reddit.com/r/%2/hot?limit=%1&%3"
|
||||
#define REDDIT_API_HOT "https://oauth.reddit.com%1hot?limit=%2&count=%3&g=%4"
|
||||
|
||||
#define REDDIT_DEFAULT_BATCH_SIZE 100
|
||||
#define REDDIT_MAX_BATCH_SIZE 999
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
#include "services/abstract/category.h"
|
||||
#include "services/reddit/definitions.h"
|
||||
#include "services/reddit/redditserviceroot.h"
|
||||
#include "services/reddit/redditsubscription.h"
|
||||
|
||||
#include <QHttpMultiPart>
|
||||
#include <QJsonArray>
|
||||
|
@ -178,12 +179,14 @@ QList<Feed*> RedditNetworkFactory::subreddits(const QNetworkProxy& custom_proxy)
|
|||
for (const QJsonValue& sub_val : root_doc["data"].toObject()["children"].toArray()) {
|
||||
const auto sub_obj = sub_val.toObject()["data"].toObject();
|
||||
|
||||
Feed* new_sub = new Feed();
|
||||
RedditSubscription* new_sub = new RedditSubscription();
|
||||
|
||||
new_sub->setCustomId(sub_obj["id"].toString());
|
||||
new_sub->setTitle(sub_obj["title"].toString());
|
||||
new_sub->setDescription(sub_obj["public_description"].toString());
|
||||
|
||||
new_sub->setPrefixedName(sub_obj["url"].toString());
|
||||
|
||||
QIcon icon;
|
||||
QString icon_url = sub_obj["community_icon"].toString();
|
||||
|
||||
|
@ -232,9 +235,14 @@ QList<Message> RedditNetworkFactory::hot(const QString& sub_name, const QNetwork
|
|||
QString after;
|
||||
QList<Message> msgs;
|
||||
|
||||
int desired_count = batchSize();
|
||||
|
||||
do {
|
||||
int next_batch = desired_count <= 0 ? 100 : std::min(100, desired_count - msgs.size());
|
||||
|
||||
QByteArray output;
|
||||
QString final_url = QSL(REDDIT_API_HOT).arg(QString::number(100), sub_name, QSL("GLOBAL"));
|
||||
QString final_url =
|
||||
QSL(REDDIT_API_HOT).arg(sub_name, QString::number(next_batch), QString::number(msgs.size()), QSL("GLOBAL"));
|
||||
|
||||
if (!after.isEmpty()) {
|
||||
final_url += QSL("&after=%1").arg(after);
|
||||
|
@ -282,7 +290,7 @@ QList<Message> RedditNetworkFactory::hot(const QString& sub_name, const QNetwork
|
|||
}
|
||||
}
|
||||
}
|
||||
while (!after.isEmpty());
|
||||
while (!after.isEmpty() && (desired_count <= 0 || desired_count > msgs.size()));
|
||||
|
||||
// posty dle jmena redditu
|
||||
// https://oauth.reddit.com/<SUBREDDIT>/new
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
#include "services/reddit/redditcategory.h"
|
||||
#include "services/reddit/redditentrypoint.h"
|
||||
#include "services/reddit/redditnetworkfactory.h"
|
||||
#include "services/reddit/redditsubscription.h"
|
||||
|
||||
#include <QFileDialog>
|
||||
|
||||
|
@ -71,7 +72,7 @@ QList<Message> RedditServiceRoot::obtainNewMessages(Feed* feed,
|
|||
Q_UNUSED(tagged_messages)
|
||||
Q_UNUSED(feed)
|
||||
|
||||
QList<Message> messages = m_network->hot(feed->title(), networkProxy());
|
||||
QList<Message> messages = m_network->hot(qobject_cast<RedditSubscription*>(feed)->prefixedName(), networkProxy());
|
||||
|
||||
return messages;
|
||||
}
|
||||
|
@ -101,7 +102,7 @@ bool RedditServiceRoot::supportsCategoryAdding() const {
|
|||
|
||||
void RedditServiceRoot::start(bool freshly_activated) {
|
||||
if (!freshly_activated) {
|
||||
DatabaseQueries::loadRootFromDatabase<RedditCategory, Feed>(this);
|
||||
DatabaseQueries::loadRootFromDatabase<RedditCategory, RedditSubscription>(this);
|
||||
loadCacheFromFile();
|
||||
}
|
||||
|
||||
|
|
|
@ -4,8 +4,28 @@
|
|||
|
||||
#include "services/reddit/redditserviceroot.h"
|
||||
|
||||
RedditSubscription::RedditSubscription(RootItem* parent) : Feed(parent) {}
|
||||
RedditSubscription::RedditSubscription(RootItem* parent) : Feed(parent), m_prefixedName(QString()) {}
|
||||
|
||||
RedditServiceRoot* RedditSubscription::serviceRoot() const {
|
||||
return qobject_cast<RedditServiceRoot*>(getParentServiceRoot());
|
||||
}
|
||||
|
||||
QString RedditSubscription::prefixedName() const {
|
||||
return m_prefixedName;
|
||||
}
|
||||
|
||||
void RedditSubscription::setPrefixedName(const QString& prefixed_name) {
|
||||
m_prefixedName = prefixed_name;
|
||||
}
|
||||
|
||||
QVariantHash RedditSubscription::customDatabaseData() const {
|
||||
QVariantHash data;
|
||||
|
||||
data.insert(QSL("prefixed_name"), prefixedName());
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
void RedditSubscription::setCustomDatabaseData(const QVariantHash& data) {
|
||||
setPrefixedName(data.value(QSL("prefixed_name")).toString());
|
||||
}
|
||||
|
|
|
@ -8,12 +8,21 @@
|
|||
class RedditServiceRoot;
|
||||
|
||||
class RedditSubscription : public Feed {
|
||||
Q_OBJECT
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit RedditSubscription(RootItem* parent = nullptr);
|
||||
|
||||
RedditServiceRoot* serviceRoot() const;
|
||||
|
||||
QString prefixedName() const;
|
||||
void setPrefixedName(const QString& prefixed_name);
|
||||
|
||||
virtual QVariantHash customDatabaseData() const;
|
||||
virtual void setCustomDatabaseData(const QVariantHash& data);
|
||||
|
||||
private:
|
||||
QString m_prefixedName;
|
||||
};
|
||||
|
||||
#endif // REDDITSUBSCRIPTION_H
|
||||
|
|
Loading…
Add table
Reference in a new issue