News API Tutorial

google sheet
Dengan skrip ini Anda dapat memasukkan kata kunci dan mendapatkan daftar artikel berita dalam bahasa apa pun, diurutkan berdasarkan kebaruan, relevansi, atau popularitas. Plus, dapatkan metrik Otoritas Domain Moz dan Otoritas Halaman untuk setiap URL. News API terbatas hingga 100 permintaan per hari.


Daftar ke News API: https://newsapi.org/register untuk mendapatkan API Key. Untuk proyek pribadi, API memiliki paket gratis, dibatasi hingga 100 permintaan per hari.

News Artikel Script


/**
 * Returns the news based on a given topic. 
 *
 * @param {"Bitcoin"} keyword - input the topic keyword.  
 * @param {true} intitle - [OPTIONAL] input true if you want to search in article titles only.
 * @param {"2021-06-01"} datefrom - [OPTIONAL] input a date you want to start the search from (limited to one month old).
 * @param {"2021-06-13"} dateto - [OPTIONAL] input a date for the newest article allowed.
 * @param {"en"} language - [OPTIONAL] input the 2-letter ISO-639-1 code. Default set to 'en'. 
 * @param {"publishedAt"} sortby - [OPTIONAL] input how how you would like the news to be sorted. 'relevancy', 'popularity' or 'publishedAt'(default). 
 * @customfunction
 */


function news(keyword, exactmatch, intitle, datefrom, dateto, language, sortby) {


	const moz = (urls) => {

		// Moz API details
		const accessID = '';
		const secret = '';
		const authHeader = 'Basic ' + Utilities.base64Encode(accessID + ':' + secret);

		const requestBody = {
			'targets': urls
		};

		const options = {
			'method': 'POST',
			'muteHttpExceptions': true,
			'headers': {
				'Authorization': authHeader
			},
			'payload': JSON.stringify(requestBody)
		};

		const response = UrlFetchApp.fetch("https://lsapi.seomoz.com/v2/url_metrics", options);

		const json = JSON.parse(response.getContentText());

		const results = json.results;

		let rows = [],
			data;

    // loop through API response and push DA and PA to array 
		for (i = 0; i < results.length; i++) {
			data = results[i];
			let pa = !data.page_authority ? 0 : data.page_authority;
			rows.push([data.domain_authority, pa]);
		}

		return rows;

	}


	// add API key from https://newsapi.org/
	const apiKey = '';

	// set default values
	intitle = intitle || false;
	exactmatch = exactmatch || false;
	datefrom = datefrom || false;
	dateto = dateto || false;
	language = language || 'en';
	sortby = sortby || 'publishedAt';

	// surround with quotes if exact match
	keyword = exactmatch ? `"${keyword}"` : keyword;
	keyword = encodeURIComponent(keyword);

	// if title is true, search in article heading
	const q = intitle ? `qInTitle=${keyword}` : `q=${keyword}`;

	// if from or to values are false, fetch using default date values
	const newsFetch = !datefrom || !dateto ? `https://newsapi.org/v2/everything?${q}&apiKey=${apiKey}&language=${language}&sortby=${sortby}` : `https://newsapi.org/v2/everything?${q}&apiKey=${apiKey}&from=${datefrom}&to=${dateto}&language=${language}&sortby=${sortby}`;


	const options = {
		'method': 'GET',
		'contentType': 'application/json',
	};

	const response = UrlFetchApp.fetch(newsFetch);
	const json = response.getContentText();
	const articles = JSON.parse(json).articles;


	let rows = [];
	let urlRows = [];
  let data;
  
  // loop through news response and details to an array
	for (i = 0; i < articles.length; i++) {
		data = articles[i];
		rows.push([data.source.name, data.title, data.description, data.author, data.url, data.publishedAt]);
		urlRows.push(data.url);
	}
  
  // if there is a response from Moz, add DA & PA to rows array
	try {
		let domainAuthority = moz(urlRows);

		for (i = 0; i < domainAuthority.length; i++) {

			rows[i].push(domainAuthority[i][0], domainAuthority[i][1]);
		}

		rows.unshift(["Publisher", "Title", "Description", "Author", "URL", "Date", "Domain Authority", "Page Authority"]);

	} catch (e) {

		rows.unshift(["Publisher", "Title", "Description", "Author", "URL", "Date"]);

	}

	return rows;
}

Perintah Google Sheetnya

=news("keyword")

Tutorial Cara Menggunakan News Script



Wayback Machine Script


/**
 * Get saved webpages for a domain from Wayback Machine in Google Sheets 
 *
 * @param {"adidas.com"} website input the domain (without the protocol)
 * @param {"20190101"} datefrom - [OPTIONAL] input the from date for the search (YYYYMMDD)
 * @param {"20200131"} dateto - [OPTIONAL] input the to date for the search (YYYYMMDD)
 * @param {"1000"} limit - [OPTIONAL] input a number to limit the number of rows returned
 * @customfunction
 */

function wayback(website, datefrom, dateto, limit) {

	// limit response to a max of 3000
	limit = limit || 3000;

	// return errors if there's no website or only one date input
	if (!website) return 'Error: you need to add a website';
	if (datefrom && !dateto) return 'Error: you need to add an end date';

	// build query dependent on whether date is selected
	const query = !datefrom ? `?url=${website}*&output=json` : `?url=${website}*&output=json&from=${datefrom}&to=${dateto}`;

	// fetch response and remove headers
	const response = UrlFetchApp.fetch(`http://web.archive.org/cdx/search/cdx${query}&limit=${limit}`);
	const content = JSON.parse(response.getContentText()).splice(1);

	if (content.length > 0) {

		// create a new array with columns needed
		const cols = () => {
			const col = [2, 3, 5];
			return content.map(r => col.map(i => r[i - 1]));
		}

		// format timestamp to readable date format
		const formatDate = (t) => {
			const year = t.substring(0, 4)
			const month = t.substring(4, 6);
			const day = t.substring(6, 8);
			return `${year}-${month}-${day}`;
		}

		//remove :80 from page if found
		const formatPage = (p) => {
			const query = ':80';
			return p.search(query) === -1 ? p : p.replace(query, '');
		}

		// create the wayback URL to view renderered page
		const formatWayback = (t, p) => {
			return `https://web.archive.org/web/${t}/${p}`;
		}

		// loop through new array, format it and add wayback URL column  
		let rows = [];
		for (i = 0; i < cols().length; i++) {

			data = cols()[i];
			const [date, page, status] = data;

			rows.push([formatDate(date), formatPage(page), status, formatWayback(date, page)]);
		}

		rows.unshift(['Date', 'URL', 'Status Code', 'Wayback URL']);

		return rows;

	} else {
		return 'Error: no data for website';
	}

}

Perintah Google Sheetnya


=wayback(domain, datefrom, dateto, limit)
LihatTutupKomentar