-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfycat-functions.js
138 lines (123 loc) · 4.1 KB
/
gfycat-functions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* form Data containing url, start, and duration parameters.
* return Confirms input, calls the create HTTP POST call, and
* updates page with location of new Gfycat Gif.
*/
function createGfycat(form) {
// Get variables from the form data
var url = form.url.value;
var start = form.start.value;
var duration = form.duration.value;
// Various checking of the input variables
// URL
if (!checkURL(url)) {
alert("[Error - 'Video URL'] Please enter a valid URL. \n" +
"Examples:\n" +
"- https://www.youtube.com/watch?v=kv1IpEsHS3A\n" +
"- https://youtu.be/SaVJyAwVUzY?t=49");
return false
}
// Start
start = findStart(start, url);
if (!start) {
alert("[Error - 'Start'] Please enter number greater than 0 for the start parameter or provide a url with a start time.");
return false
}
// Duration
if (!checkDuration(duration)) {
alert("[Error - 'Duration'] Please enter number greater than 0 for the duration parameter.");
return false
}
// Logging of input variables
console.log("URL: " + url);
console.log("Start: " + start);
console.log("Duration: " + duration);
// Create the gif
callCreateGfycat(url, start, duration);
return false;
}
/**
* url The url to fetch the video from.
* start Time to start the gif, in seconds.
* duration How long the gif will, be in seconds.
* result Calls post method to generate Gfycat gif. Updates html page with new link.
*/
function callCreateGfycat(url, start, duration) {
// Http Post's payload
var data = JSON.stringify({
"fetchUrl": url,
"noMd5" : "true",
"noResize" : "true",
"cut": {
"start":start,
"duration": duration
}
})
// jQuery Ajax call to make HTTP Post request
// https://api.jquery.com/jquery.post/
$.ajax({
type: "POST",
beforeSend: function(request) {
//request.setRequestHeader("Authentication", "Bearer " + access_token);
request.setRequestHeader("Content-Type", "application/json");
},
url: "https://api.gfycat.com/v1/gfycats",
data: data,
success: function(msg) {
// Once POST call returns, update the page
updateGfycatList(msg.gfyname)
}
})
.done(function(msg) {
console.log("[Ok] callCreateGfycat:", msg);
})
.fail(function(msg) {
console.log("[Error] callCreateGfycat:", msg);
alert("[Error] Unable to create the Gfycat gif. Please try again.");
});
}
/**
* gfyname The unique name given to the Gif by the Gfycat API.
* result Updates the HTML list with the new Gfycat's gif name and link.
*/
function updateGfycatList(gfyname) {
var list = document.getElementById('gifResults');
var newItemGfycat = document.createElement('li');
var newLinkGfycat = document.createElement('a');
newLinkGfycat.href = 'https://gfycat.com/' + gfyname
var newNameGfycat = document.createTextNode("Gfycat - " + gfyname);
newLinkGfycat.appendChild(newNameGfycat);
newItemGfycat.appendChild(newLinkGfycat);
list.appendChild(newItemGfycat);
}
/**
* result Gfycat authentication secret returned (must specify client_id and client_secret)
*/
// function authenticateGfycat() {
// console.log("Authenticating Gfycat..")
// var data = {
// "grant_type": "client_credentials",
// "client_id": {{client_id}},
// "client_secret": {{client_secret}}
// }
// $.post("https://api.gfycat.com/v1/oauth/token", JSON.stringify(data), function(data, status){
// console.log("Data: " + JSON.stringify(data) + "\nStatus: " + status);
// });
// }
/**
* prevColor Any color in Hex. Used to ensure repeat colors are not chosen.
* result Random 1 or 20 colors in Hex that is not the prevColor
*/
// function randomColor(prevColor) {
// var result;
// var colors = ["#ff0000","#ff4000","#ff8000","#ffbf00","#ffff00","#bfff00","#80ff00",
// "#40ff00","#00ff00","#00ff40","#00ff80","#00ffbf","#00ffff","#00bfff","#0080ff",
// "#0040ff", "#0000ff", "#4000ff", "#8000ff", "#bf00ff", "#ff00ff", "#ff00bf",
// "#ff0080", "#ff0040", "#ff0000"];
//
// do {
// result = Math.floor(Math.random() * colors.size())
// } while(result != prevColor);
//
// return result;
// }