Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Wikipedia Provider #21

Open
aitorllj93 opened this issue Sep 25, 2023 · 1 comment
Open

Add Wikipedia Provider #21

aitorllj93 opened this issue Sep 25, 2023 · 1 comment

Comments

@aitorllj93
Copy link

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

  • Add Wikipedia Provider

Here is the diff that solved my problem:

diff --git a/node_modules/strapi-plugin-oembed/server/services/oembed.js b/node_modules/strapi-plugin-oembed/server/services/oembed.js
index 07f789d..0b634c4 100644
--- a/node_modules/strapi-plugin-oembed/server/services/oembed.js
+++ b/node_modules/strapi-plugin-oembed/server/services/oembed.js
@@ -16,7 +16,7 @@ module.exports = (
     async fetch(url) {
       let data;
 
-      const matches = url.match(/^(https?:\/\/)?(www\.)?(youtu\.be|youtube\.com|soundcloud\.com|vimeo\.com|tiktok\.com|open\.spotify\.com)/i);
+      const matches = url.match(/^(https?:\/\/)?(www\.)?(youtu\.be|youtube\.com|soundcloud\.com|vimeo\.com|tiktok\.com|open\.spotify\.com|([a-z]{2}\.)?wikipedia\.org)/i);
 
       if (matches) {
         try {
@@ -63,6 +63,29 @@ module.exports = (
               break;
           
             default:
+              if (matches[3].endsWith('.wikipedia.org')) {
+                const Host = matches[3];
+                const [baseUrl, pageTitle] = url.split('/wiki/');
+                const apiUrl = `${baseUrl}/w/api.php?format=json&action=query&prop=extracts%7Cpageimages&exintro=&explaintext=&titles=${pageTitle}`;
+
+                const apiData = await axios.get(apiUrl, {
+                  headers: {
+                    Host
+                  }
+                }).then(res => res.data);
+
+                fetchedData = Object.values(apiData.query.pages)?.[0];
+
+                if (!fetchedData) {
+                  throw new Error('No Wikipedia page found');
+                }
+
+                title = fetchedData.title;
+                mime = 'text/html';
+                thumbnail = fetchedData.thumbnail?.source;
+                break;
+              }
+
               break;
           }
             
@@ -75,6 +98,9 @@ module.exports = (
           }
           
         } catch (error) {
+          if (!error || !error.response) {
+            throw new Error(error);
+          }
           if (error.response.status === 404) {
             data = {
               error: 'This URL can\'t be found'

This issue body was partially generated by patch-package.

@nicolashmln
Copy link
Owner

nicolashmln commented Dec 9, 2023

Please open a PR if you want wikipedia to be added on the providers' list

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants