diff --git a/README.md b/README.md index 4db3480..c385553 100644 --- a/README.md +++ b/README.md @@ -235,6 +235,7 @@ Provider and query may be given as positionals after `s` / `-s`, or with `-P` / | `magdeleine` | [Magdeleine](https://magdeleine.co) | Hand-picked free high-resolution stock photography | Direct master resolution photo downloads. | | `splitshire` | [SplitShire](https://splitshire.com) | Free stock photos and artwork by Daniel Nanescu & community | Direct high-resolution photo downloads. | | `deviantart` | [DeviantArt](https://deviantart.com) | Creative reference photos, stock imagery, and digital art | Direct image downloads via Wixmp CDN. | +| `negativespace` | [NegativeSpace](https://negativespace.co) | Free high-resolution CC0 stock photography | Direct master full-resolution photo downloads. | List providers at runtime via `ast help` (printed under **PROVIDERS**). @@ -298,6 +299,7 @@ src/ magdeleine.zig splitshire.zig deviantart.zig + negativespace.zig assets/ README banner and static assets docs/ Provider implementation notes install.sh curl | bash installer (Linux release binaries) diff --git a/docs/providers.md b/docs/providers.md index ffb6117..54c730b 100644 --- a/docs/providers.md +++ b/docs/providers.md @@ -170,3 +170,16 @@ This document summarizes the network endpoints, transport models, and image down * Image path extracted directly from signed Wixmp CDN URLs. * **Download**: Direct GET on Wixmp CDN URL with browser headers. * **License**: Free Creative Community Stock / Artwork Reference. + +--- + +## 15. NegativeSpace (`negativespace.co`) + +* **Search**: `GET https://negativespace.co/?s={query}` +* **Format**: WordPress photography feed with card images and post links. +* **Extraction**: + * Title extracted and unescaped from image `alt` attribute. + * Slug ID parsed from post permalink. + * Master full-resolution URL extracted by stripping geometry suffixes (`-1062x708`) from `wp-content/uploads/` image URLs. +* **Download**: Direct GET on original full-resolution master photo JPEG/PNG (up to 20+ MB). +* **License**: CC0 / 100% Free High-Resolution Stock Photos. diff --git a/docs/stock_photo_sites.md b/docs/stock_photo_sites.md index db5f737..b296042 100644 --- a/docs/stock_photo_sites.md +++ b/docs/stock_photo_sites.md @@ -8,7 +8,7 @@ This document tracks all 24 stock photo websites listed in the [GrayGrids 21+ Be - **Total Sites Listed**: 24 - **Implemented & Working**: 13 (`unsplash`, `isorepublic`, `picjumbo`, `foodiesfeed`, `picography`, `gratisography`, `startupstockphotos`, `burst`, `jaymantri`, `publicdomainarchive`, `magdeleine`, `splitshire`, `deviantart`) -- *(Additional Provider Implemented in `ast`)*: `aura` ([Aura.build](https://www.aura.build)) +- *(Additional Providers Implemented in `ast`)*: `aura` ([Aura.build](https://www.aura.build)), `negativespace` ([NegativeSpace.co](https://negativespace.co)) - **Unimplemented / Blocked / Pending**: 11 --- diff --git a/src/commands/search.zig b/src/commands/search.zig index 75a1762..0d31479 100644 --- a/src/commands/search.zig +++ b/src/commands/search.zig @@ -17,6 +17,7 @@ const publicdomainarchive = @import("../providers/publicdomainarchive.zig"); const magdeleine = @import("../providers/magdeleine.zig"); const splitshire = @import("../providers/splitshire.zig"); const deviantart = @import("../providers/deviantart.zig"); +const negativespace = @import("../providers/negativespace.zig"); const stdio = @import("../stdio.zig"); const Allocator = std.mem.Allocator; const Io = std.Io; @@ -80,6 +81,9 @@ fn doSearch( if (std.ascii.eqlIgnoreCase(provider_id, "deviantart")) { return deviantart.search(client, allocator, query, limit); } + if (std.ascii.eqlIgnoreCase(provider_id, "negativespace")) { + return negativespace.search(client, allocator, query, limit); + } return error.UnknownProvider; } @@ -133,6 +137,9 @@ fn doDownload( if (std.ascii.eqlIgnoreCase(provider_id, "deviantart")) { return deviantart.download(client, allocator, io, a, output_dir); } + if (std.ascii.eqlIgnoreCase(provider_id, "negativespace")) { + return negativespace.download(client, allocator, io, a, output_dir); + } return error.UnknownProvider; } @@ -151,6 +158,7 @@ fn doPrompt(provider_id: []const u8, a: Asset) ?[]const u8 { if (std.ascii.eqlIgnoreCase(provider_id, "magdeleine")) return magdeleine.getPrompt(a); if (std.ascii.eqlIgnoreCase(provider_id, "splitshire")) return splitshire.getPrompt(a); if (std.ascii.eqlIgnoreCase(provider_id, "deviantart")) return deviantart.getPrompt(a); + if (std.ascii.eqlIgnoreCase(provider_id, "negativespace")) return negativespace.getPrompt(a); return a.prompt orelse a.description; } @@ -169,6 +177,7 @@ fn doUrl(provider_id: []const u8, a: Asset) ?[]const u8 { if (std.ascii.eqlIgnoreCase(provider_id, "magdeleine")) return magdeleine.getUrl(a); if (std.ascii.eqlIgnoreCase(provider_id, "splitshire")) return splitshire.getUrl(a); if (std.ascii.eqlIgnoreCase(provider_id, "deviantart")) return deviantart.getUrl(a); + if (std.ascii.eqlIgnoreCase(provider_id, "negativespace")) return negativespace.getUrl(a); return a.image_url; } diff --git a/src/interactive.zig b/src/interactive.zig index 2a1fcf2..822b3c4 100644 --- a/src/interactive.zig +++ b/src/interactive.zig @@ -17,6 +17,7 @@ const publicdomainarchive = @import("providers/publicdomainarchive.zig"); const magdeleine = @import("providers/magdeleine.zig"); const splitshire = @import("providers/splitshire.zig"); const deviantart = @import("providers/deviantart.zig"); +const negativespace = @import("providers/negativespace.zig"); const registry = @import("providers/registry.zig"); const stdio = @import("stdio.zig"); const Allocator = std.mem.Allocator; @@ -77,6 +78,9 @@ fn searchProvider( if (std.ascii.eqlIgnoreCase(provider_id, "deviantart")) { return deviantart.search(client, allocator, query, config.default_limit); } + if (std.ascii.eqlIgnoreCase(provider_id, "negativespace")) { + return negativespace.search(client, allocator, query, config.default_limit); + } return error.UnknownProvider; } @@ -129,6 +133,9 @@ fn downloadAsset( if (std.ascii.eqlIgnoreCase(provider_id, "deviantart")) { return deviantart.download(client, allocator, io, a, config.default_output_dir); } + if (std.ascii.eqlIgnoreCase(provider_id, "negativespace")) { + return negativespace.download(client, allocator, io, a, config.default_output_dir); + } return error.UnknownProvider; } @@ -147,6 +154,7 @@ fn promptOf(provider_id: []const u8, a: Asset) ?[]const u8 { if (std.ascii.eqlIgnoreCase(provider_id, "magdeleine")) return magdeleine.getPrompt(a); if (std.ascii.eqlIgnoreCase(provider_id, "splitshire")) return splitshire.getPrompt(a); if (std.ascii.eqlIgnoreCase(provider_id, "deviantart")) return deviantart.getPrompt(a); + if (std.ascii.eqlIgnoreCase(provider_id, "negativespace")) return negativespace.getPrompt(a); return a.prompt; } @@ -165,6 +173,7 @@ fn urlOf(provider_id: []const u8, a: Asset) ?[]const u8 { if (std.ascii.eqlIgnoreCase(provider_id, "magdeleine")) return magdeleine.getUrl(a); if (std.ascii.eqlIgnoreCase(provider_id, "splitshire")) return splitshire.getUrl(a); if (std.ascii.eqlIgnoreCase(provider_id, "deviantart")) return deviantart.getUrl(a); + if (std.ascii.eqlIgnoreCase(provider_id, "negativespace")) return negativespace.getUrl(a); return a.image_url; } diff --git a/src/providers/negativespace.zig b/src/providers/negativespace.zig new file mode 100644 index 0000000..dded167 --- /dev/null +++ b/src/providers/negativespace.zig @@ -0,0 +1,294 @@ +//! NegativeSpace provider — high-resolution CC0 stock photos for personal and commercial use. +//! Search: GET https://negativespace.co/?s={query} +//! Download: Direct master-resolution JPEG/PNG from Flywheel / WordPress media storage. + +const std = @import("std"); +const asset_mod = @import("../asset.zig"); +const http_client = @import("../http_client.zig"); +const download_mod = @import("../download.zig"); +const Allocator = std.mem.Allocator; +const Io = std.Io; +const Asset = asset_mod.Asset; +const SearchResult = asset_mod.SearchResult; + +pub const id = "negativespace"; +pub const name = "NegativeSpace"; + +fn htmlUnescape(allocator: Allocator, s: []const u8) ![]u8 { + var list: std.ArrayList(u8) = .empty; + errdefer list.deinit(allocator); + var i: usize = 0; + while (i < s.len) { + if (s[i] == '&') { + if (std.mem.startsWith(u8, s[i..], "&")) { + try list.append(allocator, '&'); + i += 5; + continue; + } + if (std.mem.startsWith(u8, s[i..], """)) { + try list.append(allocator, '"'); + i += 6; + continue; + } + if (std.mem.startsWith(u8, s[i..], "–")) { + try list.append(allocator, '-'); + i += 7; + continue; + } + if (std.mem.startsWith(u8, s[i..], "—")) { + try list.append(allocator, '-'); + i += 7; + continue; + } + if (std.mem.startsWith(u8, s[i..], "&")) { + try list.append(allocator, '&'); + i += 6; + continue; + } + if (std.mem.startsWith(u8, s[i..], "'") or std.mem.startsWith(u8, s[i..], "'")) { + try list.append(allocator, '\''); + i += if (std.mem.startsWith(u8, s[i..], "'")) 5 else 6; + continue; + } + } + try list.append(allocator, s[i]); + i += 1; + } + return list.toOwnedSlice(allocator); +} + +fn stripGeometrySuffix(allocator: Allocator, url: []const u8) ![]u8 { + const last_dot = std.mem.lastIndexOfScalar(u8, url, '.') orelse return try allocator.dupe(u8, url); + const ext = url[last_dot..]; + const before_ext = url[0..last_dot]; + + const last_dash = std.mem.lastIndexOfScalar(u8, before_ext, '-') orelse return try allocator.dupe(u8, url); + const dim_part = before_ext[last_dash + 1 ..]; + + if (std.mem.indexOfScalar(u8, dim_part, 'x')) |x_pos| { + const w_str = dim_part[0..x_pos]; + const h_str = dim_part[x_pos + 1 ..]; + if (w_str.len > 0 and h_str.len > 0) { + var all_digits = true; + for (w_str) |c| { + if (!std.ascii.isDigit(c)) { + all_digits = false; + break; + } + } + for (h_str) |c| { + if (!std.ascii.isDigit(c)) { + all_digits = false; + break; + } + } + if (all_digits) { + return try std.fmt.allocPrint(allocator, "{s}{s}", .{ before_ext[0..last_dash], ext }); + } + } + } + + return try allocator.dupe(u8, url); +} + +fn parseSearch(allocator: Allocator, html: []const u8, limit: u32) ![]Asset { + var assets: std.ArrayList(Asset) = .empty; + errdefer { + for (assets.items) |*a| a.deinit(allocator); + assets.deinit(allocator); + } + + var seen: std.StringHashMapUnmanaged(void) = .empty; + defer { + var it = seen.keyIterator(); + while (it.next()) |k| allocator.free(k.*); + seen.deinit(allocator); + } + + const img_marker = "wp-content/uploads/"; + var pos: usize = 0; + while (assets.items.len < limit) { + const found = std.mem.indexOfPos(u8, html, pos, img_marker) orelse break; + pos = found + img_marker.len; + + // Find enclosing + const img_start = std.mem.lastIndexOfScalar(u8, html[0..found], '<') orelse continue; + if (!std.mem.startsWith(u8, html[img_start..], "') orelse continue; + const img_tag = html[img_start .. img_end + 1]; + + // Extract src + const src_key = "src=\""; + const src_i = std.mem.indexOf(u8, img_tag, src_key) orelse continue; + const src_start = src_i + src_key.len; + const src_end = std.mem.indexOfScalarPos(u8, img_tag, src_start, '"') orelse continue; + const src_val = img_tag[src_start..src_end]; + if (std.mem.indexOf(u8, src_val, "wp-content/uploads/") == null) continue; + if (std.mem.indexOf(u8, src_val, "logo") != null) continue; + + // Extract alt + const alt_key = "alt=\""; + const alt_val: []const u8 = blk: { + if (std.mem.indexOf(u8, img_tag, alt_key)) |ai| { + const as = ai + alt_key.len; + const ae = std.mem.indexOfScalarPos(u8, img_tag, as, '"') orelse break :blk ""; + break :blk img_tag[as..ae]; + } + break :blk ""; + }; + + // Find enclosing + const a_search_start = if (img_start > 500) img_start - 500 else 0; + const a_chunk = html[a_search_start..img_start]; + const href_val: []const u8 = blk: { + if (std.mem.lastIndexOf(u8, a_chunk, " 0 and std.mem.startsWith(u8, href_val, "https://negativespace.co/")) { + var trimmed = href_val["https://negativespace.co/".len..]; + trimmed = std.mem.trim(u8, trimmed, "/"); + if (trimmed.len > 0) slug_id = trimmed; + } + if (slug_id.len == 0) { + const last_slash = std.mem.lastIndexOfScalar(u8, src_val, '/') orelse continue; + slug_id = src_val[last_slash + 1 ..]; + } + + const pid = try allocator.dupe(u8, slug_id); + errdefer allocator.free(pid); + + if (seen.contains(pid)) { + allocator.free(pid); + continue; + } + try seen.put(allocator, try allocator.dupe(u8, pid), {}); + + const title_unesc = try htmlUnescape(allocator, if (alt_val.len > 0) alt_val else slug_id); + defer allocator.free(title_unesc); + + const desc = if (title_unesc.len > 0) title_unesc else pid; + const desc_owned = try allocator.dupe(u8, desc); + errdefer allocator.free(desc_owned); + const prompt_owned = try allocator.dupe(u8, desc); + errdefer allocator.free(prompt_owned); + + const master_url = try stripGeometrySuffix(allocator, src_val); + errdefer allocator.free(master_url); + + const thumb_owned = try allocator.dupe(u8, src_val); + errdefer allocator.free(thumb_owned); + const prov_owned = try allocator.dupe(u8, id); + errdefer allocator.free(prov_owned); + const auth_owned = try allocator.dupe(u8, "NegativeSpace"); + errdefer allocator.free(auth_owned); + + const page_url = if (href_val.len > 0) href_val else master_url; + var meta_aw: Io.Writer.Allocating = .init(allocator); + defer meta_aw.deinit(); + try meta_aw.writer.print("{{\"source\":\"negativespace\",\"page\":\"{s}\"}}", .{page_url}); + const meta = try meta_aw.toOwnedSlice(); + + try assets.append(allocator, .{ + .id = pid, + .description = desc_owned, + .prompt = prompt_owned, + .image_url = master_url, + .thumbnail_url = thumb_owned, + .provider = prov_owned, + .author = auth_owned, + .width = null, + .height = null, + .metadata_json = meta, + }); + } + + return try assets.toOwnedSlice(allocator); +} + +pub fn search( + client: *std.http.Client, + allocator: Allocator, + query: []const u8, + limit: u32, +) !SearchResult { + const trimmed = std.mem.trim(u8, query, " \t\n\r"); + if (trimmed.len == 0) return error.EmptyQuery; + + const encoded_query = try http_client.queryEscape(allocator, trimmed); + defer allocator.free(encoded_query); + + const search_url = try std.fmt.allocPrint(allocator, "https://negativespace.co/?s={s}", .{encoded_query}); + defer allocator.free(search_url); + + var resp = try http_client.get(client, allocator, search_url, .{ + .accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", + .referer = "https://negativespace.co/", + .max_redirects = 5, + }); + defer resp.deinit(); + if (resp.status != 200) return error.HttpStatus; + + const lim = @min(@max(limit, 1), 50); + const assets = try parseSearch(allocator, resp.body, lim); + + return .{ + .assets = assets, + .total = null, + .provider = try allocator.dupe(u8, id), + .query = try allocator.dupe(u8, query), + .allocator = allocator, + }; +} + +pub fn download( + client: *std.http.Client, + allocator: Allocator, + io: Io, + a: Asset, + output_dir: []const u8, +) !download_mod.Saved { + const url = a.image_url orelse return error.NoUrl; + const body = http_client.getBody(client, allocator, url, .{ + .referer = "https://negativespace.co/", + .accept = "image/avif,image/webp,image/apng,image/*,*/*;q=0.8", + .max_redirects = 5, + }) catch |err| switch (err) { + error.RateLimited => return error.RateLimited, + error.HttpStatus => return error.HttpStatus, + error.OutOfMemory => return error.OutOfMemory, + else => return error.Network, + }; + defer allocator.free(body); + if (body.len == 0) return error.HttpStatus; + + try download_mod.ensureDir(io, output_dir); + const ext = download_mod.guessExtension(url); + const path = try download_mod.uniquePath(allocator, io, output_dir, a.id, ext); + errdefer allocator.free(path); + const cwd = Io.Dir.cwd(); + cwd.writeFile(io, .{ .sub_path = path, .data = body }) catch return error.Io; + return .{ + .path = path, + .filename = std.fs.path.basename(path), + .bytes = body.len, + .allocator = allocator, + }; +} + +pub fn getPrompt(a: Asset) ?[]const u8 { + if (a.prompt) |p| if (p.len > 0) return p; + if (a.description.len > 0) return a.description; + return null; +} + +pub fn getUrl(a: Asset) ?[]const u8 { + return a.image_url; +} diff --git a/src/providers/registry.zig b/src/providers/registry.zig index 2b50c79..6d8abee 100644 --- a/src/providers/registry.zig +++ b/src/providers/registry.zig @@ -15,6 +15,7 @@ const publicdomainarchive = @import("publicdomainarchive.zig"); const magdeleine = @import("magdeleine.zig"); const splitshire = @import("splitshire.zig"); const deviantart = @import("deviantart.zig"); +const negativespace = @import("negativespace.zig"); pub const ProviderInfo = struct { id: []const u8, @@ -37,6 +38,7 @@ pub fn list() []const ProviderInfo { .{ .id = magdeleine.id, .name = magdeleine.name }, .{ .id = splitshire.id, .name = splitshire.name }, .{ .id = deviantart.id, .name = deviantart.name }, + .{ .id = negativespace.id, .name = negativespace.name }, }; } @@ -55,5 +57,5 @@ pub fn displayName(id: []const u8) ?[]const u8 { } pub fn availableIds() []const u8 { - return "aura, unsplash, isorepublic, picjumbo, foodiesfeed, picography, gratisography, startupstockphotos, burst, jaymantri, publicdomainarchive, magdeleine, splitshire, deviantart"; + return "aura, unsplash, isorepublic, picjumbo, foodiesfeed, picography, gratisography, startupstockphotos, burst, jaymantri, publicdomainarchive, magdeleine, splitshire, deviantart, negativespace"; }