Skip to content

Commit

Permalink
Use wikimedia commons instead of language specific wikipedia.
Browse files Browse the repository at this point in the history
  • Loading branch information
HarelM committed Nov 9, 2024
1 parent f1fbab2 commit 631e9f1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ import { HttpTestingController, provideHttpClientTesting } from "@angular/common
import { inject, TestBed } from "@angular/core/testing";

import { ImageAttributionService } from "./image-attribution.service";
import { ResourcesService } from "./resources.service";

describe("ImageAttributionService", () => {

beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{ provide: ResourcesService, useValue: { getCurrentLanguageCodeSimplified: () => "en" } },
ImageAttributionService,
provideHttpClient(withInterceptorsFromDi()),
provideHttpClientTesting()
Expand Down Expand Up @@ -38,7 +36,7 @@ describe("ImageAttributionService", () => {
it("should fetch data from wikipedia when getting wikimedia image", inject([ImageAttributionService, HttpTestingController],
async (service: ImageAttributionService, mockBackend: HttpTestingController) => {
const promise = service.getAttributionForImage("https://upload.wikimedia.org/wikipedia/commons/thumb/a/a1/IHM_Image.jpeg");
mockBackend.match(r => r.url.startsWith("https://en.wikipedia.org/"))[0].flush({
mockBackend.match(r => r.url.startsWith("https://commons.wikimedia.org/"))[0].flush({
query: {
pages: {
"-1": {
Expand All @@ -58,13 +56,13 @@ describe("ImageAttributionService", () => {

expect(response).not.toBeNull();
expect(response.author).toBe("hello");
expect(response.url).toBe("https://en.wikipedia.org/wiki/File:IHM_Image.jpeg");
expect(response.url).toBe("https://commons.wikimedia.org/wiki/File:IHM_Image.jpeg");
}));

it("should remove html tags and get the value inside", inject([ImageAttributionService, HttpTestingController],
async (service: ImageAttributionService, mockBackend: HttpTestingController) => {
const promise = service.getAttributionForImage("https://upload.wikimedia.org/wikipedia/commons/thumb/a/a1/IHM_Image.jpeg");
mockBackend.match(r => r.url.startsWith("https://en.wikipedia.org/"))[0].flush({
mockBackend.match(r => r.url.startsWith("https://commons.wikimedia.org/"))[0].flush({
query: {
pages: {
"-1": {
Expand All @@ -84,13 +82,13 @@ describe("ImageAttributionService", () => {

expect(response).not.toBeNull();
expect(response.author).toBe("hello");
expect(response.url).toBe("https://en.wikipedia.org/wiki/File:IHM_Image.jpeg");
expect(response.url).toBe("https://commons.wikimedia.org/wiki/File:IHM_Image.jpeg");
}));

it("should remove html tags, tabs and get the value inside", inject([ImageAttributionService, HttpTestingController],
async (service: ImageAttributionService, mockBackend: HttpTestingController) => {
const promise = service.getAttributionForImage("https://upload.wikimedia.org/wikipedia/commons/thumb/a/a1/IHM_Image.jpeg");
mockBackend.match(r => r.url.startsWith("https://en.wikipedia.org/"))[0].flush({
mockBackend.match(r => r.url.startsWith("https://commons.wikimedia.org/"))[0].flush({
query: {
pages: {
"-1": {
Expand All @@ -110,14 +108,14 @@ describe("ImageAttributionService", () => {

expect(response).not.toBeNull();
expect(response.author).toBe("hello world");
expect(response.url).toBe("https://en.wikipedia.org/wiki/File:IHM_Image.jpeg");
expect(response.url).toBe("https://commons.wikimedia.org/wiki/File:IHM_Image.jpeg");
}));

// Based on https://upload.wikimedia.org/wikipedia/commons/b/b5/Historical_map_series_for_the_area_of_Al-Manara%2C_Palestine_%281870s%29.jpg
it("should remove html tags and get the value inside for multiple html tags", inject([ImageAttributionService, HttpTestingController],
async (service: ImageAttributionService, mockBackend: HttpTestingController) => {
const promise = service.getAttributionForImage("https://upload.wikimedia.org/wikipedia/commons/thumb/a/a1/IHM_Image.jpeg");
mockBackend.match(r => r.url.startsWith("https://en.wikipedia.org/"))[0].flush({
mockBackend.match(r => r.url.startsWith("https://commons.wikimedia.org/"))[0].flush({
query: {
pages: {
"-1": {
Expand Down Expand Up @@ -147,13 +145,13 @@ describe("ImageAttributionService", () => {
"PEF Survey of Palestine\n" +
"Survey of PalestineOverlay from Palestine Open Maps\n" +
"OpenStreetMap");
expect(response.url).toBe("https://en.wikipedia.org/wiki/File:IHM_Image.jpeg");
expect(response.url).toBe("https://commons.wikimedia.org/wiki/File:IHM_Image.jpeg");
}));

it("should return null when getting wikimedia image without artist", inject([ImageAttributionService, HttpTestingController],
async (service: ImageAttributionService, mockBackend: HttpTestingController) => {
const promise = service.getAttributionForImage("https://upload.wikimedia.org/wikipedia/commons/thumb/a/a1/IHM_Image.jpeg");
mockBackend.match(r => r.url.startsWith("https://en.wikipedia.org/"))[0].flush({
mockBackend.match(r => r.url.startsWith("https://commons.wikimedia.org/"))[0].flush({
query: {
pages: {
"-1": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { HttpClient } from "@angular/common/http";
import { inject, Injectable } from "@angular/core";
import { firstValueFrom, timeout } from "rxjs";

import { ResourcesService } from "./resources.service";

export type ImageAttribution = {
author: string;
url: string;
Expand All @@ -13,7 +11,6 @@ export type ImageAttribution = {
export class ImageAttributionService {
private attributionImageCache = new Map<string, ImageAttribution>();

private readonly resources = inject(ResourcesService);
private readonly httpClient = inject(HttpClient);

private extractPlainText(html: string): string {
Expand Down Expand Up @@ -43,17 +40,16 @@ export class ImageAttributionService {
}

const imageName = imageUrl.split("/").pop();
const language = this.resources.getCurrentLanguageCodeSimplified();
const address = `https://${language}.wikipedia.org/w/api.php?action=query&prop=imageinfo&iiprop=extmetadata&format=json&origin=*` +
const address = `https://commons.wikimedia.org/w/api.php?action=query&prop=imageinfo&iiprop=extmetadata&format=json&origin=*` +
`&titles=File:${imageName}`;
try {
const response: any = await firstValueFrom(this.httpClient.get(address).pipe(timeout(3000)));
const extmetadata = response.query.pages[-1].imageinfo[0].extmetadata;
const extmetadata = response.query.pages[Object.keys(response.query.pages)[0]].imageinfo[0].extmetadata;
if (extmetadata?.Artist.value) {
const author = this.extractPlainText(extmetadata.Artist.value as string);
const imageAttribution = {
author,
url: `https://${language}.wikipedia.org/wiki/File:${imageName}`
url: `https://commons.wikimedia.org/wiki/File:${imageName}`
};
this.attributionImageCache.set(imageUrl, imageAttribution);
return imageAttribution;
Expand Down

0 comments on commit 631e9f1

Please sign in to comment.