RandomWallpaperGnome3/randomwallpaper@iflow.space/sourceAdapter.js

138 lines
3.6 KiB
JavaScript
Raw Normal View History

2016-04-08 16:40:52 +00:00
const Lang = imports.lang;
2017-07-19 13:20:29 +00:00
const Self = imports.misc.extensionUtils.getCurrentExtension();
2016-04-08 16:40:52 +00:00
// network requests
const Soup = imports.gi.Soup;
const Json = imports.gi.Json;
const HistoryModule = Self.imports.history;
2017-07-19 13:20:29 +00:00
const LoggerModule = Self.imports.logger;
let BaseAdapter = new Lang.Class({
Name: "BaseAdapter",
2017-07-20 11:48:13 +00:00
logger: null,
2017-07-19 13:20:29 +00:00
_init: function () {
this.logger = new LoggerModule.Logger('RWG3', 'BaseAdapter');
},
/**
* Retrieves a new url for an image and calls the given callback with an HistoryEntry as parameter.
* @param callback
*/
requestRandomImage: function (callback) {
this.logger.error("requestRandomImage not implemented");
callback(null);
2017-07-20 11:48:13 +00:00
},
fileName: function(uri)
{
let base = new String(uri).substring(uri.lastIndexOf('/') + 1);
return base;
},
2017-07-19 13:20:29 +00:00
});
2016-04-08 16:40:52 +00:00
let DesktopperAdapter = new Lang.Class({
Name: "DesktopperAdapter",
2017-07-19 13:20:29 +00:00
Extends: BaseAdapter,
2017-02-03 16:38:01 +00:00
/*
fetch a random image url from desktopper.cc
and call callback function with the URL of the image
*/
requestRandomImage: function (callback) {
2016-04-08 16:40:52 +00:00
let session = new Soup.SessionAsync();
let message = Soup.Message.new('GET', 'https://api.desktoppr.co/1/wallpapers/random');
2016-04-08 16:40:52 +00:00
let parser = new Json.Parser();
2017-07-20 11:48:13 +00:00
session.queue_message(message, (session, message) => {
2016-04-08 16:40:52 +00:00
parser.load_from_data(message.response_body.data, -1);
let data = parser.get_root().get_object();
2016-04-08 16:40:52 +00:00
let response = data.get_object_member('response');
let imageUrl = response.get_object_member('image').get_string_member('url');
if (callback) {
let historyEntry = new HistoryModule.HistoryEntry(null, 'desktopper.co', imageUrl);
historyEntry.source.sourceUrl = 'https://www.desktoppr.co/';
callback(historyEntry);
}
2016-04-08 16:40:52 +00:00
});
}
});
2016-04-08 16:40:52 +00:00
let WallheavenAdapter = new Lang.Class({
Name: "WallheavenAdapter",
2017-07-19 13:20:29 +00:00
Extends: BaseAdapter,
2016-04-08 16:40:52 +00:00
// query options
options: {
'q': '',
'purity': '110', // SFW, sketchy
'sorting': 'random',
'category': '111', // General, Anime, People
'resolutions': ['1920x1200', '2560x1440']
2016-04-08 16:40:52 +00:00
},
2017-02-03 16:38:01 +00:00
/*
fetch a random image url from wallheaven.cc with the given options
and call callback function with the URL of the image
*/
requestRandomImage: function (callback) {
2016-04-08 16:40:52 +00:00
let session = new Soup.SessionAsync();
2017-02-03 16:38:01 +00:00
2016-04-08 16:40:52 +00:00
let options = this.options;
let optionsString = "";
2016-04-08 16:40:52 +00:00
2017-07-20 11:48:13 +00:00
for (let key in options) {
2016-04-08 16:40:52 +00:00
if (options.hasOwnProperty(key)) {
if (Array.isArray(options[key])) {
2017-02-03 16:38:01 +00:00
optionsString += key + "=" + options[key].join() + "&";
2016-04-08 16:40:52 +00:00
} else {
optionsString += key + "=" + options[key] + "&";
}
}
}
// remove last '&'
optionsString = optionsString.slice(0, -1);
2016-04-08 16:40:52 +00:00
let url = 'http://alpha.wallhaven.cc/search?' + optionsString;
2017-02-03 16:38:01 +00:00
2016-04-08 16:40:52 +00:00
let message = Soup.Message.new('GET', url);
2017-07-20 11:48:13 +00:00
session.queue_message(message, (session, message) => {
2016-04-08 16:40:52 +00:00
let body = message.response_body.data;
2017-02-03 16:38:01 +00:00
let urlArray = body.match(new RegExp(/http[s]*:\/\/alpha.wallhaven.cc\/wallpaper\/[0-9]+/g));
2016-04-08 16:40:52 +00:00
// remove dublicates from array
let uniqueUrlArray = urlArray.filter(function (item, pos) {
2016-04-08 16:40:52 +00:00
return urlArray.indexOf(item) == pos;
});
// get a random entry from the array
2017-07-20 11:48:13 +00:00
let url = uniqueUrlArray[Math.floor(Math.random() * uniqueUrlArray.length)];
2016-04-08 16:40:52 +00:00
message = Soup.Message.new('GET', url);
2017-07-20 11:48:13 +00:00
session.queue_message(message, () => {
2016-04-08 16:40:52 +00:00
let body = message.response_body.data;
let imageUrl = body.match(new RegExp(/\/\/wallpapers.wallhaven.cc\/wallpapers\/full\/.*?"/))[0];
imageUrl = imageUrl.slice(0, -1);
2017-02-03 16:38:01 +00:00
imageUrl = 'http:' + imageUrl;
2016-04-08 16:40:52 +00:00
if (callback) {
let historyEntry = new HistoryModule.HistoryEntry(null, 'wallhaven.cc', imageUrl);
historyEntry.source.sourceUrl = 'https://alpha.wallhaven.cc/';
callback(historyEntry);
}
2016-04-08 16:40:52 +00:00
})
});
}
});