Merge pull request #40 from oskarkook/develop

Add Reddit support
This commit is contained in:
Wolfgang Rumpler 2018-08-19 10:32:58 +02:00 committed by GitHub
commit 3e11305aab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 184 additions and 29 deletions

View file

@ -17,6 +17,7 @@ const RWG_SETTINGS_SCHEMA = 'org.gnome.shell.extensions.space.iflow.randomwallpa
const RWG_SETTINGS_SCHEMA_DESKTOPPER = 'org.gnome.shell.extensions.space.iflow.randomwallpaper.desktopper';
const RWG_SETTINGS_SCHEMA_UNSPLASH = 'org.gnome.shell.extensions.space.iflow.randomwallpaper.unsplash';
const RWG_SETTINGS_SCHEMA_WALLHAVEN = 'org.gnome.shell.extensions.space.iflow.randomwallpaper.wallhaven';
const RWG_SETTINGS_SCHEMA_REDDIT = 'org.gnome.shell.extensions.space.iflow.randomwallpaper.reddit';
const RWG_SETTINGS_SCHEMA_GENERIC_JSON = 'org.gnome.shell.extensions.space.iflow.randomwallpaper.genericJSON';
const LoggerModule = Self.imports.logger;
@ -44,6 +45,7 @@ var RandomWallpaperSettings = new Lang.Class({
desktopperSettings: null,
unsplashSettings: null,
wallhavenSettings: null,
redditSettings: null,
genericJsonSettings: null,
_wallpaperController: null,
@ -73,6 +75,11 @@ var RandomWallpaperSettings = new Lang.Class({
this.wallhavenSettings = this._builder.get_object('wallhaven-settings');
this.bindWallhaven();
// Reddit Settings
this._reddit_settings = Convenience.getSettings(RWG_SETTINGS_SCHEMA_REDDIT);
this.redditSettings = this._builder.get_object('reddit-settings');
this.bindReddit();
// Generic JSON Settings
this._generic_json_settings = Convenience.getSettings(RWG_SETTINGS_SCHEMA_GENERIC_JSON);
this.genericJsonSettings = this._builder.get_object('generic-json-settings');
@ -102,7 +109,10 @@ var RandomWallpaperSettings = new Lang.Class({
case 2: // wallhaven
this.currentSourceSettingsWidget = this.wallhavenSettings;
break;
case 3: // generic JSON
case 3: // reddit
this.currentSourceSettingsWidget = this.redditSettings;
break;
case 4: // generic JSON
this.currentSourceSettingsWidget = this.genericJsonSettings;
break;
default:
@ -227,6 +237,17 @@ var RandomWallpaperSettings = new Lang.Class({
Gio.SettingsBindFlags.DEFAULT);
},
bindReddit: function () {
this._reddit_settings.bind('subreddits',
this._builder.get_object('reddit-subreddits'),
'text',
Gio.SettingsBindFlags.DEFAULT);
this._reddit_settings.bind('allow-sfw',
this._builder.get_object('reddit-allow-sfw'),
'active',
Gio.SettingsBindFlags.DEFAULT);
},
bindGenericJSON: function () {
this._builder.get_object('generic-json-docs-link').set_label("More information here");
this._generic_json_settings.bind('generic-json-request-url',

View file

@ -5,7 +5,8 @@
<value value='0' nick='unsplash'/>
<value value='1' nick='desktoppr'/>
<value value='2' nick='wallhaven'/>
<value value='3' nick='genericJSON'/>
<value value='3' nick='reddit'/>
<value value='4' nick='genericJSON'/>
</enum>
<schema path="/org/gnome/shell/extensions/space-iflow-randomwallpaper/"
@ -162,6 +163,20 @@
</key>
</schema>
<schema path="/org/gnome/shell/extensions/space-iflow-randomwallpaper/reddit/"
id='org.gnome.shell.extensions.space.iflow.randomwallpaper.reddit'>
<key type='s' name='subreddits'>
<default>""</default>
<summary>Subreddits</summary>
<description>These subreddits will be searched.</description>
</key>
<key type='b' name='allow-sfw'>
<default>false</default>
<summary>SFW</summary>
<description>Whether safe images are allowed.</description>
</key>
</schema>
<schema path="/org/gnome/shell/extensions/space-iflow-randomwallpaper/genericJSON/"
id='org.gnome.shell.extensions.space.iflow.randomwallpaper.genericJSON'>
<key type='s' name='generic-json-request-url'>

View file

@ -199,6 +199,93 @@ You can also define a prefix that will be added to the image URL.</property>
<property name="step_increment">1</property>
<property name="page_increment">10</property>
</object>
<object class="GtkLabel" id="no-settings">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">10</property>
<property name="margin_right">10</property>
<property name="margin_top">10</property>
<property name="margin_bottom">10</property>
<property name="label" translatable="yes">No Settings Available</property>
<style>
<class name="dim-label"/>
</style>
</object>
<object class="GtkImage" id="open-icon">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stock">gtk-open</property>
</object>
<object class="GtkBox" id="reddit-settings">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">10</property>
<property name="margin_right">10</property>
<property name="margin_top">10</property>
<property name="margin_bottom">10</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="label" translatable="yes">Subreddits (separate with a comma)</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="reddit-subreddits">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="placeholder_text" translatable="yes">wallpaper, wallpapers, earthporn</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkButtonBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="layout_style">start</property>
<child>
<object class="GtkCheckButton" id="reddit-allow-sfw">
<property name="label" translatable="yes">SFW (Safe for work)</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
<object class="GtkImage" id="refresh-icon">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stock">gtk-refresh</property>
</object>
<object class="GtkImage" id="refresh-icon1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stock">gtk-delete</property>
</object>
<object class="GtkNotebook" id="main-widget">
<property name="width_request">400</property>
<property name="height_request">600</property>
@ -727,6 +814,7 @@ You can also define a prefix that will be added to the image URL.</property>
<item id="unsplash">unsplash.com</item>
<item id="desktoppr">desktoppr.co</item>
<item id="wallhaven">alpha.wallhaven.cc</item>
<item id="reddit">reddit</item>
<item id="genericJSON" translatable="yes">Generic JSON</item>
</items>
</object>
@ -1213,33 +1301,6 @@ You can also define a prefix that will be added to the image URL.</property>
</packing>
</child>
</object>
<object class="GtkLabel" id="no-settings">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">10</property>
<property name="margin_right">10</property>
<property name="margin_top">10</property>
<property name="margin_bottom">10</property>
<property name="label" translatable="yes">No Settings Available</property>
<style>
<class name="dim-label"/>
</style>
</object>
<object class="GtkImage" id="open-icon">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stock">gtk-open</property>
</object>
<object class="GtkImage" id="refresh-icon">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stock">gtk-refresh</property>
</object>
<object class="GtkImage" id="refresh-icon1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stock">gtk-delete</property>
</object>
<object class="GtkAdjustment" id="unsplash-image-height">
<property name="lower">1</property>
<property name="upper">1000000</property>

View file

@ -8,6 +8,7 @@ const Json = imports.gi.Json;
const RWG_SETTINGS_SCHEMA_DESKTOPPER = 'org.gnome.shell.extensions.space.iflow.randomwallpaper.desktopper';
const RWG_SETTINGS_SCHEMA_UNSPLASH = 'org.gnome.shell.extensions.space.iflow.randomwallpaper.unsplash';
const RWG_SETTINGS_SCHEMA_WALLHAVEN = 'org.gnome.shell.extensions.space.iflow.randomwallpaper.wallhaven';
const RWG_SETTINGS_SCHEMA_REDDIT = 'org.gnome.shell.extensions.space.iflow.randomwallpaper.reddit';
const RWG_SETTINGS_SCHEMA_GENERIC_JSON = 'org.gnome.shell.extensions.space.iflow.randomwallpaper.genericJSON';
const SettingsModule = Self.imports.settings;
@ -352,6 +353,59 @@ var WallhavenAdapter = new Lang.Class({
}
});
var RedditAdapter = new Lang.Class({
Name: "RedditAdapter",
Extends: BaseAdapter,
_settings: null,
_init: function () {
this.parent();
this._settings = new SettingsModule.Settings(RWG_SETTINGS_SCHEMA_REDDIT);
},
requestRandomImage: function (callback) {
let session = new Soup.SessionAsync();
const subreddits = this._settings.get('subreddits', 'string').split(',').map(s => s.trim()).join('+');
const require_sfw = this._settings.get('allow-sfw', 'boolean');
const url = encodeURI(`https://www.reddit.com/r/${subreddits}.json`);
let message = Soup.Message.new('GET', url);
if (message === null) {
this._error("Could not create request.", callback);
return;
}
session.queue_message(message, (session, message) => {
try {
const submissions = JSON.parse(message.response_body.data).data.children.filter(child => {
if(child.data.post_hint !== 'image') return false;
if(require_sfw) return child.data.over_18 === false;
return true;
});
if(submissions.length === 0) {
this._error("No suitable submissions found!", callback);
return;
}
const random = Math.floor(Math.random() * submissions.length);
const submission = submissions[random].data;
const imageDownloadUrl = submission.preview.images[0].source.url;
if (callback) {
let historyEntry = new HistoryModule.HistoryEntry(null, 'reddit', imageDownloadUrl);
historyEntry.source.sourceUrl = 'https://www.reddit.com/' + submission.permalink;
callback(historyEntry);
}
} catch (e) {
this._error("Could not create request. (" + e + ")", callback);
return;
}
});
}
});
var GenericJsonAdapter = new Lang.Class({
Name: "GenericJsonAdapter",
Extends: BaseAdapter,

View file

@ -54,6 +54,7 @@ var WallpaperController = new Lang.Class({
this._desktopperAdapter = new SourceAdapter.DesktopperAdapter();
this._unsplashAdapter = new SourceAdapter.UnsplashAdapter();
this._wallhavenAdapter = new SourceAdapter.WallhavenAdapter();
this._redditAdapter = new SourceAdapter.RedditAdapter();
this._genericJsonAdapter = new SourceAdapter.GenericJsonAdapter();
this._updateHistory();
@ -98,6 +99,9 @@ var WallpaperController = new Lang.Class({
this.imageSourceAdapter = this._wallhavenAdapter;
break;
case 3:
this.imageSourceAdapter = this._redditAdapter;
break;
case 4:
this.imageSourceAdapter = this._genericJsonAdapter;
break;
default: