Usage Twitter Stream api for search - twitter

i`m trying use Twitter Stream Api for searching some hashtags in Google Spreadsheet. Twitter search api useless cause i wanna trak retweet count too. My function sample here. Can anybody explain me what i must do for working well..
function miniSearch(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sumSheet = ss.getSheetByName("Readme/Settings");
// Authorize to Twitter
var oauthConfig = UrlFetchApp.addOAuthService("twitter");
oauthConfig.setAccessTokenUrl("https://api.twitter.com/oauth/access_token");
oauthConfig.setRequestTokenUrl("https://api.twitter.com/oauth/request_token");
oauthConfig.setAuthorizationUrl("https://api.twitter.com/oauth/authorize");
oauthConfig.setConsumerKey(TWITTER_CONSUMER_KEY);
oauthConfig.setConsumerSecret(TWITTER_CONSUMER_SECRET);
// "twitter" value must match the argument to "addOAuthService" above.
var options = {
'method': 'POST',
"oAuthServiceName" : "twitter",
"oAuthUseToken" : "always"
};
var url = "https://stream.twitter.com/1/statuses/filter.json?track="+"twitterapi";
var response = UrlFetchApp.fetch(url, options);
var tweets = JSON.parse(response.getContentText());
sumSheet.getRange('B8').setValue(tweets[0]["text"]);
}
this function return error code 504;

I don't think Google Apps Script can keep a persistent HTTP connection open which is resulting in the 504 (See Twitter Streaming APIs Doc)
[I've a basic retweet counter in this Google Spreadsheet Template (TAGS v4.0). The filterUnique formula uses this code (the pseudocode is strip out any links from tweet text then extract 1st 90% of text (to take account of any old style RT+ annotation), then if not in unique array add or add 1 to existing value):
function filterUnique(tweets){
var output = [];
var temp = {};
for (i in tweets){
if (i>0){
var tmp = tweets[i][0];
var urlPattern = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|!:,.;]*[-A-Z0-9+&##\/%=~_|])/ig;
tmp = tmp.replace(urlPattern,"")
tmp = tmp.substring(0,parseInt(tmp.length*0.9));
if (temp[tmp] == undefined){
temp[tmp] = [tweets[i][0],0];
}
temp[tmp] = [tweets[i][0],temp[tmp][1]+1];
}
}
for (i in temp){
output.push([temp[i][0],temp[i][1]]);
}
output.sort(function(a,b) {
return b[1]-a[1];
});
return output.slice(0, 12);
}
]

Related

Q: How can I import a table from IAAI site to google sheets?

I have a website link which is this
https://www.iaai.com/saleslist/711/09032021
and I am trying to import all the info in this webpage using
=importxml("https://www.iaai.com/saleslist/711/09032021","/html/body/section/main/section[3]/div/div/div[1]/section[2]/div/table")
but it does not work.
I know I am learning basic stuff and at the very beginning.
Thank you in advance
To retrieve a table, the syntax is
=importhtml("https://www.iaai.com/saleslist/711/09032021","table",1)
But in this case, you will be above max authorized size. You then need a script:
function importTableHTML(url) {
var html = '<table' + UrlFetchApp.fetch(url).getContentText().replace(/(\r\n|\n|\r)/gm,"").match(/(?<=\<table).*(?=\<\/table)/g) + '</table>';
var trs = [...html.matchAll(/<tr[\s\S\w]+?<\/tr>/g)];
var data = [];
for (var i=0;i<trs.length;i++){
var tds = [...trs[i][0].matchAll(/<(td)[\s\S\w]+?<\/(td)>/g)];
var prov = [];
for (var j=0;j<tds.length;j++){
donnee=tds[j][0];
prov.push(stripTags(donnee.replace(/[ ]{2,}/gm," ")));
}
data.push(prov);
}
return(data);
}
function stripTags(body) {
var regex = /(<([^>]+)>)/ig;
return body.replace(regex,"");
}
https://docs.google.com/spreadsheets/d/1yb4Zc3gsBbnM9FD-T_vU6YivVJuMlaJJTnMAOCtwy3M/copy

Writing a google sheets function to only apply to changed rows

I've recently integrated Twilio with one of my sheets to automatically send messages to phone numbers. An example process is below...
Spreadsheet contains several hundred rows with specific message details in each that twilio sends to a specific phone number
Function is set to trigger on sheet edit
User enters a phone number in rows 3, 59, and 148 that have the unique message details to send
Function triggers and sends messages
The problem is that the function I have will search the entire sheet for a phone number. I would like to write the function so it immediately knows which cells have been edited (with the new phone number) and only runs the function on those rows. So instead of taking 30 minutes to comb through every row looking for a phone number, it only looks at those rows that changed.
Any idea how I can make this happen? The function I have so far is below. I know the line I need to change, but just dont know the code to put.
function sendSms(to, body) {
var messages_url = "INSERT TWILIO URL HERE";
var payload = {
"To": to,
"Body" : body,
"From" : "(315) 888-4032"
};
var options = {
"method" : "post",
"payload" : payload
};
options.headers = {
"Authorization" : "Basic " + Utilities.base64Encode("PRIVATE INFO")
};
UrlFetchApp.fetch(messages_url, options);
}
function sendAll() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = sheet.getLastRow() - 1;
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
try {
response_data = sendSms(row[0], row[1]);
status = "sent";
} catch(err) {
Logger.log(err);
status = "error";
}
sheet.getRange(startRow + Number(i), 3).setValue(status);
}
}
function myFunction() {
sendAll();
}

Pulling a feed of youtube videos with statistics and duration data

I'm currently trying to use the Youtube V3 API to generate a feed of the most recently uploaded videos in a channel. Currently I've got this mostly working, however I can't for the life of me get two important bits of data pulled through - video views and video duration. I understand that these bits of data belong to the "videos" endpoint, but i'm not sure how to integrate this into the Javascript API example that was provided on Google's Youtube API website. Here is my code below - any help would be greatly appreciated:
// Define some variables used to remember state.
var playlistId, nextPageToken, prevPageToken;
// After the API loads, call a function to get the uploads playlist ID.
function handleAPILoaded() {
requestUserUploadsPlaylistId();
}
// Call the Data API to retrieve the playlist ID that uniquely identifies the
// list of videos uploaded to the currently authenticated user's channel.
function requestUserUploadsPlaylistId() {
// See https://developers.google.com/youtube/v3/docs/channels/list
var request = gapi.client.youtube.channels.list({
mine: true,
part: 'contentDetails, statistics'
});
request.execute(function(response) {
playlistId = response.result.items[0].contentDetails.relatedPlaylists.uploads;
requestVideoPlaylist(playlistId);
});
}
// Retrieve the list of videos in the specified playlist.
function requestVideoPlaylist(playlistId, pageToken) {
$('#video-container').html('');
var requestOptions = {
playlistId: playlistId,
part: 'snippet, contentDetails',
maxResults: 10
};
if (pageToken) {
requestOptions.pageToken = pageToken;
}
var request = gapi.client.youtube.playlistItems.list(requestOptions);
request.execute(function(response) {
// Only show pagination buttons if there is a pagination token for the
// next or previous page of results.
nextPageToken = response.result.nextPageToken;
var nextVis = nextPageToken ? 'visible' : 'hidden';
$('#next-button').css('visibility', nextVis);
prevPageToken = response.result.prevPageToken
var prevVis = prevPageToken ? 'visible' : 'hidden';
$('#prev-button').css('visibility', prevVis);
var playlistItems = response.result.items;
if (playlistItems) {
$.each(playlistItems, function(index, item) {
displayResult(item.snippet);
displayContentDetails(item.contentDetails);
displayStats(item.statistics);
});
} else {
$('#video-container').html('Sorry you have no uploaded videos');
}
});
}
// Create a listing for a video.
function displayResult(videoSnippet) {
var title = videoSnippet.title;
var videoId = videoSnippet.resourceId.videoId;
var publishedAt = videoSnippet.publishedAt;
var videoThumbURL = videoSnippet.thumbnails.high.url;
var publishedUTC = new Date(publishedAt);
var publishedDay = publishedUTC.getUTCDay();
var publishedMonth = publishedUTC.getUTCMonth();
var publishedYear = publishedUTC.getUTCFullYear();
var a = moment(publishedUTC);
var b = moment(Date.now());
var timeFrom = a.from(b);
$('#video-container').append('<li><img src='+videoThumbURL+'><br><p>' + title + ' - ' + timeFrom + '</p></li>');
}
// Create a listing for a video.
function displayContentDetails(stuff) {
var videoEndsAt = stuff.duration;
$('#video-container li').append('<span class="endsAt">'+ videoEndsAt +'</span>');
}
function displayStats(vidStats) {
var videoStats = vidStats.viewCount;
$('#video-container li').append('<span class="videoStats">'+ videoStats +'</span>');
}
// Retrieve the next page of videos in the playlist.
function nextPage() {
requestVideoPlaylist(playlistId, nextPageToken);
}
// Retrieve the previous page of videos in the playlist.
function previousPage() {
requestVideoPlaylist(playlistId, prevPageToken);
}
Although you said channel the code shows you pulling from a playlist, which does not have the data you want.
For each video from the playlist, you'll need to save the videoID, then make call(s) to the Videos: list endpoint (up to 50 ids at a time) with the 'parts' parameter of 'snippet,contentDetails,statistics'. Those API results will have the data you want.
Thanks #Johnh10, you were right, this is what I ended up doing.
var videoStatsEndpoint = "https://www.googleapis.com/youtube/v3/videos?part=statistics";
var videoToQuery = "&id="+videoId+"&key=";
var apiKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
var videoViews = 0;
jQuery.get(videoStatsEndpoint + videoToQuery + apiKey, function(data){
videoViews = data.items[0].statistics.viewCount;
console.log(videoViews);
});

Is OAuth a must before using Google+API

I m trying to use get and list method with google plus comment. In official site it said (All API calls require either an OAuth 2.0 token or an API key. ) and I have tried send GET request without the step of OAuth it works it returns json format data. My question is OAuth must require before using google+ API?
It depends on exactly what data you're trying to get.
https://developers.google.com/+/api/oauth documents the benefits of using OAuth, but in general, if you want to get private profile data, or if you wish to use the /me/ URL shortcut, you will need to use OAuth and may, if you wish, use an App Key in addition. If all you're interested in is public data, you can use the App Key.
The short answer to whether you can do it is that you can get comments from Google+ without OAuth.
As for the how would you do this, I'm not sure which language you're doing this in but the following code shows how this is done in JavaScript.
The API calls used here can be experimented with in the API explorer:
Listing Activities
Listing Comments
A demo of this code is here.
You will need an API key (the simple key) for a project with the Google+ APIs from the Google APIs console. When you set up the project, you will only need to enable the Google+ API from the services section.
First, grab the activities using the public data API:
// Gets the activities for a profile
function getActivities(profileID){
var activities = null;
var URL = "https://www.googleapis.com/plus/v1/people/" + profileID + "/activities/public?alt=json&key=" + key;
var request = new XMLHttpRequest();
request.open('GET', URL, false);
request.send(); // because of "false" above, will block until the request is done
// and status is available. Not recommended, however it works for simple cases.
if (request.status === 200) {
if (debug) console.log("retrieved activities \n\n");
var activities = jQuery.parseJSON(request.responseText).items;
console.log("Discovered " + activities.length + " activities");
}else{
handleRequestIssue(request);
}
return activities;
}
The following code loops through the activities
for (var i=0; i < activities.length; i++) {
console.log("trying to do something with an activity: " + i);
var activity = activities[i];
console.log(activity.id);
}
Next, you can use the activity IDs to retrieve the comments per activity:
function getCommentsForActivity(activityID){
var comments = "";
var URL = "https://www.googleapis.com/plus/v1/activities/" + activityID + "/comments?alt=json&key=" + key;
var request = new XMLHttpRequest();
request.open('GET', URL, false);
request.send(); // because of "false" above, will block until the request is done
// and status is available. Not recommended, however it works for simple cases.
if (request.status === 200) {
if (debug) console.log(request.responseText);
var comments = jQuery.parseJSON(request.responseText).items;
if (debug){
for (comment in comments){
console.log(comment);
}
}
}else{
handleRequestIssue(request);
}
return comments;
}
function manualTrigger(){
var activities = getActivities("109716647623830091721");
}
The following code brings it all together and retrieves activities and comments for a specific post:
$(document).ready(function () {
var renderMe = "";
var activities = getActivities("109716647623830091721");
console.log("activities retrieved: " + activities.length);
for (var i=0; i < activities.length; i++) {
console.log("trying to do something with an activity: " + i);
var activity = activities[i];
renderMe += "<br/><div class=\"article\"><p>" + activity.title + "</p>";
console.log(activity.id);
// get comments
var comments = getCommentsForActivity(activity.id);
for (var j=0; j<comments.length; j++){
renderMe += "<br/><div class=\"comment\">" + comments[j].object.content + "</div>";
}
renderMe += "</div>";
}
console.log("I'm done");
document.getElementById("ac").innerHTML = renderMe;
});

Create a POST body using Google Apps Script

I'm trying to create a Google Apps Script that adds a new owner to the user's Google calendar. The first code block below works correctly (returns the calendar ACL in JSON format). How can I add a new user to the acl using Google Apps Script? The second code block shows my attempt to insert a new rule into the acl.
function getCalendarACL() {
// Get Calendar ID, script user's email, and the API Key for access to Calendar API
var calId = 'abc123#group.calendar.google.com';
var userEmail = Session.getActiveUser().getEmail();
var API_KEY = '012345abc123';
// Get authorization to access the Google Calendar API
var apiName = 'calendar';
var scope = 'https://www.googleapis.com/auth/calendar';
var fetchArgs = googleOAuth_(apiName, scope);
// Get the authorization information and the given calendar
fetchArgs.method = 'GET';
// Get the requested content (the ACL for the calendar)
var base = 'https://www.googleapis.com/calendar/v3/calendars/';
var url = base + calId + '/acl?key=' + API_KEY;
var content = UrlFetchApp.fetch(url, fetchArgs).getContentText();
Logger.log(content);
}
function googleOAuth_(name,scope) {
var oAuthConfig = UrlFetchApp.addOAuthService(name);
oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oAuthConfig.setConsumerKey("anonymous");
oAuthConfig.setConsumerSecret("anonymous");
return {oAuthServiceName:name, oAuthUseToken:"always"};
}
Here's the second code block that returns server error 400 ("parse error"):
function insertRule() {
// Get Calendar ID, script user's email, and the API Key for access to Calendar API
var calId = 'abc123#group.calendar.google.com';
var userEmail = Session.getActiveUser().getEmail();
var API_KEY = '012345abc123';
var newUserEmail = 'person#gmail.com';
// Get authorization to access the Google Calendar API
var apiName = 'calendar';
var scope = 'https://www.googleapis.com/auth/calendar';
var fetchArgs = googleOAuth_(apiName, scope);
// Get the authorization information and the given calendar
fetchArgs.method = 'GET';
// Create the POST request body
var rawXML = "<entry xmlns='http://www.w3.org/2005/Atom' " +
"xmlns:gAcl='http://schemas.google.com/acl/2007'>" +
"<category scheme='http://schemas.google.com/g/2005#kind'" +
"term='http://schemas.google.com/acl/2007#accessRule'/>" +
"<gAcl:scope type='user' value='"+newUserEmail+"'></gAcl:scope>" +
"<gAcl:role='writer'>" +
"</gAcl:role>" +
"</entry>";
// Get the requested content (the ACL for the calendar)
var base = 'https://www.googleapis.com/calendar/v3/calendars/';
var url = base + calId + '/acl?key=' + API_KEY;
var content = UrlFetchApp.fetch(url, fetchArgs).getContentText();
Logger.log(content);
}
I would encode the newUserEmail variable like this encodeURIComponent(newUserEmail) when making the rawXML string and then retry.
You are using:
fetchArgs.method = 'GET';
but at the Acl:insert page found here says this:
HTTP Request
POST https://www.googleapis.com/calendar/v3/calendars/calendarId/acl
So, it should be,
fetchArgs.method = 'POST';

Resources