Skip to content

Commit

Permalink
add widget; fix issues about danbooru
Browse files Browse the repository at this point in the history
  • Loading branch information
Gandum2077 committed Mar 13, 2020
1 parent a0cbf7f commit 7623b3c
Show file tree
Hide file tree
Showing 12 changed files with 378 additions and 44 deletions.
4 changes: 2 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"info": {
"name": "JSBooru",
"url": "",
"version": "2.0.4",
"version": "2.1.0",
"author": "Gandum2077",
"website": "https://github.com/Gandum2077/JSBooru",
"types": 1
"types": 0
},
"settings": {
"minSDKVer": "2.5.1",
Expand Down
9 changes: 7 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
const app = require("scripts/app");
app.init();
if ($app.env === $env.app) {
const app = require("scripts/app");
await app.init();
} else {
const widget = require("scripts/widget");
await widget.init();
}
6 changes: 6 additions & 0 deletions prefs.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
"type": "boolean",
"key": "safe_search",
"value": true
},
{
"title": "Filter Nonauthorized Images",
"type": "boolean",
"key": "filter_nonauthorized_images",
"value": true
}
]
},
Expand Down
7 changes: 6 additions & 1 deletion scripts/booru/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ function safeSearchFilter(items) {
return items.filter(n => n.rating === "s");
}

function nonauthorizedFilter(items) {
return items.filter(n => n.previewUrl)
}

module.exports = {
safeSearchFilter
safeSearchFilter,
nonauthorizedFilter
};
11 changes: 10 additions & 1 deletion scripts/booru/fix.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,16 @@ const fixFunctions = {
item.sampleUrl = `https://realbooru.com/samples/${directory}/sample_${basename}.jpg`;
}
return item;
}
},
"danbooru.donmai.us": item => {
if (!item.fileUrl) {
item.fileUrl = item.data.file_url || null
}
if (!item.previewUrl) {
item.previewUrl = item.data.preview_file_url || null
}
return item;
},
};

function fix(item) {
Expand Down
63 changes: 38 additions & 25 deletions scripts/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const {
generatorForSite,
generatorForFavorites
} = require("./booru/generator");
const { safeSearchFilter } = require("./booru/filter");
const { safeSearchFilter, nonauthorizedFilter } = require("./booru/filter");
const { checkRandomSupport } = require("./utils/utils");
const SubController = require("./subController");

Expand Down Expand Up @@ -73,14 +73,21 @@ class Controller {
});
this.views.thumbnailsViewBooru = new ThumbnailsView({
layout: (make, view) => {
make.left.right.bottom.inset(0)
make.top.inset(36)
make.left.right.bottom.inset(0);
make.top.inset(36);
},
events: {
itemSize: function(sender, indexPath) {
const index = indexPath.item
const info = classThis.booruItems[index]
return $size(info.width, info.height)
const index = indexPath.item;
const info = classThis.booruItems[index];
const width = info.width;
let height = info.height;
if (height > width * 2) {
height = width * 2;
} else if (height < width / 2) {
height = width / 2;
}
return $size(width, height);
},
pulled: async function(sender) {
if (classThis.isLoading) {
Expand Down Expand Up @@ -114,11 +121,7 @@ class Controller {
}
const result = await classThis.generatorBooru.next();
if (!result.done) {
if ($prefs.get("safe_search")) {
classThis.booruItems.push(...safeSearchFilter(result.value));
} else {
classThis.booruItems.push(...result.value);
}
classThis.booruItems.push(...classThis._filter(result.value));
classThis.views.thumbnailsViewBooru.items = classThis.booruItems;
}
sender.endFetchingMore();
Expand All @@ -127,14 +130,21 @@ class Controller {
});
this.views.thumbnailsViewFavorites = new ThumbnailsView({
layout: (make, view) => {
make.left.right.bottom.inset(0)
make.top.inset(36)
make.left.right.bottom.inset(0);
make.top.inset(36);
},
events: {
itemSize: function(sender, indexPath) {
const index = indexPath.item
const info = classThis.favoritesItems[index]
return $size(info.width, info.height)
const index = indexPath.item;
const info = classThis.favoritesItems[index];
const width = info.width;
let height = info.height;
if (height > width * 2) {
height = width * 2;
} else if (height < width / 2) {
height = width / 2;
}
return $size(width, height);
},
pulled: async function(sender) {
classThis.loadFavorites({
Expand All @@ -161,7 +171,8 @@ class Controller {
const result = classThis.generatorFavorites.next();
if (!result.done) {
classThis.favoritesItems.push(...result.value);
classThis.views.thumbnailsViewFavorites.items = classThis.favoritesItems;
classThis.views.thumbnailsViewFavorites.items =
classThis.favoritesItems;
}
sender.endFetchingMore();
}
Expand All @@ -178,7 +189,7 @@ class Controller {
const tags = text.split(" ");
if (!tags || !tags.length) return;
await classThis.loadBooru({ tags });
constants.userConfig.addSearchHistory(text)
constants.userConfig.addSearchHistory(text);
}
});
this.views.searchBarFavorites = new SearchBar({
Expand All @@ -200,7 +211,7 @@ class Controller {
const tags = text.split(" ");
if (!tags || !tags.length) return;
await classThis.loadBooru({ tags });
constants.userConfig.addSearchHistory(text)
constants.userConfig.addSearchHistory(text);
}
});
}
Expand Down Expand Up @@ -441,7 +452,7 @@ class Controller {
case 2: {
this.views.tagsView.moveToFront();
$ui.title = $l10n("TAGS");
this.views.tagsView.reload()
this.views.tagsView.reload();
break;
}
default:
Expand All @@ -468,11 +479,7 @@ class Controller {
startPage
});
const result = await this.generatorBooru.next();
if ($prefs.get("safe_search")) {
this.booruItems = safeSearchFilter(result.value);
} else {
this.booruItems = result.value;
}
this.booruItems = this._filter(result.value);
this.views.thumbnailsViewBooru.items = this.booruItems;
this.views.thumbnailsViewBooru.view.scrollTo({
indexPath: $indexPath(0, 0),
Expand Down Expand Up @@ -504,6 +511,12 @@ class Controller {
this.favoritesInfo.startPage = startPage;
}

_filter(items) {
if ($prefs.get("safe_search")) items = safeSearchFilter(items);
if ($prefs.get("filter_nonauthorized_images")) items = nonauthorizedFilter(items);
return items
}

async openPrefs() {
await $prefs.open();
this.checkPrefs();
Expand Down
41 changes: 29 additions & 12 deletions scripts/utils/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ class Database {
}

insertPost({ info, favorited }) {
const { id, previewUrl: preview_url } = info
const site = info.booru.domain
const tags = ' ' + info.tags.join(' ') + ' '
const { id, previewUrl: preview_url } = info;
const site = info.booru.domain;
const tags = " " + info.tags.join(" ") + " ";
this.deletePost({ site, id });
this.db.update({
sql:
Expand Down Expand Up @@ -151,6 +151,22 @@ class Database {
};
}

getRandomPost({ limit = 4 } = {}) {
const clause =
"SELECT * FROM posts WHERE order_id IN (SELECT order_id FROM posts ORDER BY RANDOM() LIMIT ?)";
const args = [limit]
const result = this.search(clause, args);
return result.map(n => {
return {
site_id: n.site_id,
id: n.id,
thumbnail_url: n.thumbnail_url,
info: JSON.parse(n.info),
favorited: n.favorited ? true : false
};
});
}

getPostCount(favorited = true) {
const where_clause = favorited ? " WHERE favorited = 1" : "";
const clause = "SELECT COUNT() FROM posts" + where_clause;
Expand Down Expand Up @@ -229,7 +245,8 @@ class Database {

updateSavedTag({ name, title, category, favorited }) {
this.db.update({
sql: "UPDATE saved_tags SET title = ?, category = ?, favorited = ? WHERE name=?",
sql:
"UPDATE saved_tags SET title = ?, category = ?, favorited = ? WHERE name=?",
args: [title, category, favorited, name]
});
this.db.commit();
Expand Down Expand Up @@ -268,17 +285,17 @@ class Database {
}
}

safeAddSavedTag({name, title, category, favorited }) {
const result = this.searchSavedTag(name)
safeAddSavedTag({ name, title, category, favorited }) {
const result = this.searchSavedTag(name);
if (result) {
this.updateSavedTag({ name, title, category, favorited })
this.updateSavedTag({ name, title, category, favorited });
} else {
this.insertSavedTag({ name, title, category, favorited })
this.insertSavedTag({ name, title, category, favorited });
}
}

renameCategory(oldname, newname) {
if (!newname) newname = null
if (!newname) newname = null;
this.db.update({
sql: "UPDATE saved_tags SET category = ? WHERE category = ?",
args: [newname, oldname]
Expand Down Expand Up @@ -333,11 +350,11 @@ class Database {
}

safeAddCombination({ name, title }) {
const result = this.searchSavedCombination(name)
const result = this.searchSavedCombination(name);
if (result) {
this.updateSavedCombination({name, title})
this.updateSavedCombination({ name, title });
} else {
this.insertSavedCombination({name, title})
this.insertSavedCombination({ name, title });
}
}
}
Expand Down
Loading

0 comments on commit 7623b3c

Please sign in to comment.