//https://npmjs.com/package/twitter-url :)

const sfetch = require("sync-fetch");
let TwitterFetcher = {
	parseUrl: function(URL) {
		URL = URL.split("?")[0];
		let video_user = URL.split("/")[3];
		let video_id = URL.split("/")[5];
		return {
			id: video_id,
			user: video_user
		};
	},
	getCredentials: function(id) {
		//let URL_IF_NEEDED = https://ma-0.twimg.com/twitter-assets/responsive-web/web/ltr/main.5b6bf12947d7a3a6.js
		let token = process.env.twitter_key;
		let data = sfetch("https://api.twitter.com/1.1/guest/activate.json", {
			method: "POST",
			headers: {
				"Authorization": token
			}
		}).json();
		return [data.guest_token, token];
	},
	getDetails: function(id) {
		let gtoken = this.getCredentials(id);
		let data = sfetch("https://api.twitter.com/2/timeline/conversation/" + id + ".json", {
			headers: {
				"Authorization": gtoken[1],
				"x-guest-token": gtoken[0],
        "tweet_mode": "extended"
			}
		}).json();
		return data;
	},
	getDetailsConcise: function(id) {
		let data = this.getDetails(id);
		let videos = data.globalObjects.tweets[id].extended_entities.media[0].video_info;
		let title = data.globalObjects.tweets[id].text;
		let thumbnail = data.globalObjects.tweets[id].entities.media[0].media_url;
		let _ustr = data.globalObjects.tweets[id].user_id_str;
		let username = data.globalObjects.users[_ustr].name;
		return {
			"videos": videos,
			"title": title,
			"thumbnail": thumbnail,
			"user_name": username
		};
	}
};
module.exports = TwitterFetcher;