twitter

%%javascript
const settings = {
	async: true,
	crossDomain: true,
	url: 'https://twitter135.p.rapidapi.com/Search/?q=elon&count=3',
	method: 'GET',
	headers: {
		'X-RapidAPI-Key': 'befd3aa94cmsh6c15f9448db64f3p194824jsn7727f7079e12',
		'X-RapidAPI-Host': 'twitter135.p.rapidapi.com'
	}
};

$.ajax(settings).done(function (response) {
	const tweets = response.globalObjects.tweets;
    for (const tweetId in tweets) {
      const tweet = tweets[tweetId];
      const tweetText = tweet.full_text;
      console.log(tweetText);
    }
});

ig

%%javascript
const settings = {
	async: true,
	crossDomain: true,
	url: 'https://instagram-scraper-2022.p.rapidapi.com/ig/hashtag/?hashtag=messi',
	method: 'GET',
	headers: {
		'X-RapidAPI-Key': 'befd3aa94cmsh6c15f9448db64f3p194824jsn7727f7079e12',
		'X-RapidAPI-Host': 'instagram-scraper-2022.p.rapidapi.com'
	}
};

$.ajax(settings).done(function (response) {
	console.log(response.data.profile_pic_url);
});

tiktok

%%javascript
const settings = {
	async: true,
	crossDomain: true,
	url: 'https://tiktok-video-no-watermark2.p.rapidapi.com/feed/search?keywords=messi&count=1&cursor=0&region=US&publish_time=1&sort_type=1',
	method: 'GET',
	headers: {
		'X-RapidAPI-Key': 'befd3aa94cmsh6c15f9448db64f3p194824jsn7727f7079e12',
		'X-RapidAPI-Host': 'tiktok-video-no-watermark2.p.rapidapi.com'
	}
};

$.ajax(settings).done(function (response) {
	console.log(response.data.videos[0].play);
});

Incorporates twitter api, instagram api, and chatgpt api to output into table the last posts by a user on twitter and instagram

%%javascript

function createTable(jsonData) {
  const tableData = JSON.parse(jsonData);

  const table = document.createElement('table');
  table.style.borderCollapse = 'collapse';


  const thead = document.createElement('thead');
  const headerRow = document.createElement('tr');

  for (const key in tableData) {
    const th = document.createElement('th');
    th.style.border = '1px solid black';
    th.textContent = key;
    headerRow.appendChild(th);
  }

  thead.appendChild(headerRow);
  table.appendChild(thead);


  const tbody = document.createElement('tbody');
  const dataRow = document.createElement('tr');

  for (const key in tableData) {
    const td = document.createElement('td');
    td.style.border = '1px solid black';
    td.textContent = tableData[key];
    dataRow.appendChild(td);
  }

  tbody.appendChild(dataRow);
  table.appendChild(tbody);


  element.append(table);
}


class getContent {
  constructor() {
    this.user;
    this.twname;
    this.igname;
    this.tw1;
    this.tw2;
    this.tw3;
    this.ig1;
    this.ig2;
  }

  twitter(user) {
    this.user = user;
    const self = this; 

    const settingsgpt = {
      async: true,
      crossDomain: true,
      url: 'https://chatgpt53.p.rapidapi.com/',
      method: 'POST',
      headers: {
        'content-type': 'application/json',
        'X-RapidAPI-Key': '95853c1888msh0a1187e38da6414p1f7d65jsn31e03536f2e1',
        'X-RapidAPI-Host': 'chatgpt53.p.rapidapi.com'
      },
      processData: false,
      data: '{\r\n    "messages": [\r\n        {\r\n            "role": "user",\r\n            "content": "Please just return the twitter handle of ' + user + 'without the @ "\r\n        }\r\n    ]\r\n}'
    };

    $.ajax(settingsgpt).done(function (response) {
      var twitname = response.choices[0].message.content;
      self.twname = twitname;
      console.log(twitname);

      const settingsid = {
        async: true,
        crossDomain: true,
        url: 'https://twitter135.p.rapidapi.com/v2/UserByScreenName/?username=' + twitname,
        method: 'GET',
        headers: {
          'X-RapidAPI-Key': '95853c1888msh0a1187e38da6414p1f7d65jsn31e03536f2e1',
          'X-RapidAPI-Host': 'twitter135.p.rapidapi.com'
        }
      };

      $.ajax(settingsid).done(function (response) {
        var twitid = response.data.user.result.rest_id;
        console.log(twitid);

        const settingstwit = {
          async: true,
          crossDomain: true,
          url: 'https://twitter135.p.rapidapi.com/v2/UserTweets/?id=' + twitid + '&count=3',
          method: 'GET',
          headers: {
            'X-RapidAPI-Key': '95853c1888msh0a1187e38da6414p1f7d65jsn31e03536f2e1',
            'X-RapidAPI-Host': 'twitter135.p.rapidapi.com'
          }
        };

        $.ajax(settingstwit).done(function (response) {
          for (let i = 0; i < 3; i++) {
            const tweet = response.data.user.result.timeline_v2.timeline.instructions[1].entries[i].content.itemContent.tweet_results.result.legacy.full_text;
            if (i === 0) {
              self.tw1 = tweet;
              console.log(tweet);
            } else if (i === 1) {
              self.tw2 = tweet;
            } else if (i === 2) {
              self.tw3 = tweet;
            }
          }
        });
      });
    });
  }

  instagram(user) {
    const self = this;
    const settingsgpt = {
      async: true,
      crossDomain: true,
      url: 'https://chatgpt53.p.rapidapi.com/',
      method: 'POST',
      headers: {
        'content-type': 'application/json',
        'X-RapidAPI-Key': 'befd3aa94cmsh6c15f9448db64f3p194824jsn7727f7079e12',
        'X-RapidAPI-Host': 'chatgpt53.p.rapidapi.com'
      },
      processData: false,
      data: '{\r\n    "messages": [\r\n        {\r\n            "role": "user",\r\n            "content": "Please just return the instagram handle of ' + user + 'without the @ "\r\n        }\r\n    ]\r\n}'
    };

    $.ajax(settingsgpt).done(function (response) {
      var igname = response.choices[0].message.content;
      self.igname = igname;
      console.log(igname);

      const settingspost = {
        async: true,
        crossDomain: true,
        url: 'https://instagram-scraper-2022.p.rapidapi.com/ig/posts_username/?user=' + igname,
        method: 'GET',
        headers: {
          'X-RapidAPI-Key': '4d1a3c9588msh66b49b43046b339p12b3fajsnd3129dec4216',
          'X-RapidAPI-Host': 'instagram-scraper-2022.p.rapidapi.com'
        }
      };

      $.ajax(settingspost).done(function (response) {
        for (let i = 0; i < 2; i++) {
          const post = response.data.user.edge_owner_to_timeline_media.edges[i].node.thumbnail_src;
          if (i === 0) {
            self.ig1 = post;
            console.log(self.ig1);
          } else if (i === 1) {
            self.ig2 = post;
            console.log(self.ig2);
          }
        }
      });
    });
  }

  getJSON() {
    const obj = {
      name: this.user,
      twittername: this.twname,
      instagramname: this.igname,
      twitterpost1: this.tw1,
      twitterpost2: this.tw2,
      twitterpost3: this.tw3,
      instagrampost1: this.ig1,
      instagrampost2: this.ig2
    };
    const json = JSON.stringify(obj);
    return json;
  }
}


const user = new getContent();

const nameuser = 'elon musk';
user.twitter(nameuser);
user.instagram(nameuser);

// wait for ajax requests to finish
setTimeout(function () {
  const jsonData = user.getJSON();
  createTable(jsonData);
}, 10000);