@zizwar-go
@JPT
js
import UseBlogger from "use-blogger";
const variables = [
{ key: "price", type: "number" },
{ key: "color", type: "string" },
];
async function fetchData() {
const blogId = "YourBlogIdHere";
const wb = new UseBlogger({ blogId });
const res = await wb.categories(["Computers", "Mobiles"]).load(variables, 5);
console.log(res.data);
}
fetchData();
في هذا المثال، يتم تحديد المتغيرات المطلوبة باستخدام التعريفات `key` و `type`، وهي `price` و `color` و `number` و `string` على التوالي. ثم يتم استدعاء `fetchData` التي تستخدم `UseBlogger` للبحث عن مقالات من فئات "الكمبيوتر" و "الهواتف المحمولة"، ويتم تحميل المتغيرات المحددة وإرجاع أول خمسة نتائج من الاستعلام في الإخراج المطبوع في وحدة التحكم `console.log`.
تحتوي مكتبة UseBlogger على العديد من الخصائص والميزات الأخرى التي يمكن استخدامها لتخصيص الطلب، حسب الحاجة.@zizwar-go
@JPT
@Zizwar-gi
@JPT
javascript
const UseBlogger = require('use-blogger');
const axios = require('axios');
const blogId = '9012345612345432';
const variables = [
// تعريف المتغيرات التي ترغب في استخراجها هنا
];
async function fetchData() {
const wb = new UseBlogger({ blogId });
const res = await wb.load(variables, { maxResults: 5 }); // اجلب 5 مواضيع فقط
const posts = res.data;
const arabicPosts = [];
for (const post of posts) {
const id = post.id;
const title = post.title;
const content = post.content;
// استخدم API الخاص بـ GPT-3.5 Turbo لترجمة البيانات
const translatedContent = await translateText(content, 'en', 'ar');
arabicPosts.push({
id,
title,
content: translatedContent,
});
}
console.log(arabicPosts);
}
// دالة لترجمة النص باستخدام API من OpenAI
async function translateText(text, sourceLang, targetLang) {
const apiKey = 'YOUR_API_KEY'; // قم بوضع مفتاح الواجهة البرمجية الخاص بك هنا
try {
const response = await axios.post('https://api.openai.com/v1/engines/davinci-codex/completions', {
prompt: `Translate the following text from ${sourceLang} to ${targetLang}:\n\n${text}`,
max_tokens: 100,
temperature: 0.7,
top_p: 1,
n: 1,
stop: '\n',
presence_penalty: 0,
frequency_penalty: 0,
}, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
},
});
const translation = response.data.choices[0].text.trim();
return translation;
} catch (error) {
console.error('Translation failed:', error);
return '';
}
}
fetchData();
يرجى تغيير "YOUR_API_KEY" في الكود بالمفتاح الخاص بك من OpenAI API. كما قم بتعريف المتغيرات المطلوبة في المدونة في المصفوفة `variables`، وتغيير عدد المواضيع المطلوبة في `maxResults` (في هذا الحالة سيتم جلب 5 مواضيع فقط).@Zizwar-gi
@JPT
js
import UseBlogger from "use-blogger";
const variables = [
{ key: "name", type: "string" },
{ key: "images", type: "array", asArray: "images" },
];
async function myJsonBlogger() {
const data = [{ name: "test blog", id: 23, images: ["src1", "src2"] }];
const wb = new UseBlogger({ blogId: "8277077996046083588" });
wb.setData(data);
const res = await wb.load(variables);
const filteredData = res.data.map(obj => {
delete obj.id;
return obj;
});
console.log(filteredData);
console.log("length=" + filteredData?.length);
}
myJsonBlogger();
In this code, the `setData` method is used to set the data array with the value `[{ name: "test blog", id: 23, images: ["src1", "src2"] }]`. Then, after fetching the data using the `load` method, the "id" property is deleted from each object in the resulting `res.data` array. The filtered data is stored in the `filteredData` variable and logged to the console.
Please note that the UseBlogger library is not originally designed to handle static data. This modified code makes use of the library's methods to achieve the desired result with the given data, but it may not be the intended use case of the library.@Zizwar-gi
@JPT
javascript
import UseBlogger from "use-blogger";
const variables = [
{ key: "price", type: "number", regex: "price*[:=]*(.*?)[;<]" },
{ key: "discount", type: "number" },
{ key: "quantityAvailable", type: "number", as: "qnt" },
{ key: "currentPrice", type: "number" },
{ key: "sizes", type: "array", asArray: "options" },
{ key: "colors", type: "array", asArray: "options" },
];
async function fetchBlogData() {
const blogId = "8277077996046083588";
const wb = new UseBlogger({ blogId });
const res = await wb.load(variables);
console.log(res.data);
console.log("Length of data: " + res?.data.length);
}
fetchBlogData();
In the above code snippet, we create an instance of the UseBlogger class with the specified `blogId`. We pass an array of `variables` objects that define the data we want to fetch from the Blogger API. Once the `load` method is invoked, UseBlogger will fetch and organize the requested data into the `res` object. We can then access the fetched data and its length accordingly.
## Customizing the Data Request
UseBlogger provides several methods to further customize your data request. You can set specific categories or labels to fetch data from or exclude certain categories or labels. You can also define a maximum number of posts to fetch, add a search query to the request, select or exclude certain fields from the response, set the number of posts to skip, and even order the response based on a specific field.
## Conclusion
Working with the Blogger API to retrieve data from a blog becomes a breeze with the UseBlogger library. Its intuitive syntax and comprehensive set of methods allow developers to effortlessly fetch, organize, and manipulate blog data. Whether you need to extract prices, discounts, quantities, or any other variable defined within your blog posts, UseBlogger simplifies the process for you.
So why not give UseBlogger a try? Explore the library, experiment with its features, and unlock the potential of the Blogger API in a more efficient and structured manner. Happy coding!
---
By using the UseBlogger library, developers can easily retrieve data from Blogger's JSON API and harness the power of blog data in their applications. Whether you are building a personal project or a professional application, UseBlogger offers a simple and effective way to fetch and organize blog data. So why not simplify your data retrieval process with UseBlogger?