Initial commit moving front end out of main repo
This commit is contained in:
210
src/dumbyt.ts
Normal file
210
src/dumbyt.ts
Normal file
@ -0,0 +1,210 @@
|
||||
import Axios from "axios";
|
||||
|
||||
// Base URL for API queries.
|
||||
const API_BASE_URL = "/api";
|
||||
|
||||
// How many chars at most for the description.
|
||||
const max_description_length = 100;
|
||||
|
||||
// Wrapper for channels returned from our server.
|
||||
export class Channel {
|
||||
// Title of the channel according to youtube.
|
||||
public Title: string;
|
||||
|
||||
// Description of channel according to youtube.
|
||||
public Description: string;
|
||||
|
||||
// Youtube ID
|
||||
public ID: string;
|
||||
|
||||
// Tags given to the video.
|
||||
public User_Tags: Array<string>;
|
||||
|
||||
// ID's belonging to the channel.
|
||||
public Video_IDs: Array<string>;
|
||||
|
||||
// Popuplate this using data from our server.
|
||||
constructor(c : any){
|
||||
if (c == null) {
|
||||
this.Title = "NULL"
|
||||
this.Description = "NULL"
|
||||
this.ID = "NULL"
|
||||
this.User_Tags = ["NULL", "NULL", "NULL"]
|
||||
this.Video_IDs = ["NULL", "NULL"]
|
||||
} else {
|
||||
this.Title = c.title;
|
||||
this.Description = c.description;
|
||||
this.ID = c.id;
|
||||
this.User_Tags = c.user_Tags;
|
||||
this.Video_IDs = c.video_IDs;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Wrapper for videos returned from our server.
|
||||
export class Video {
|
||||
// Title of the video according to youtube.
|
||||
public Title: string;
|
||||
|
||||
// Description of video according to youtube.
|
||||
public Description: string;
|
||||
|
||||
// Youtube ID
|
||||
public ID: string;
|
||||
|
||||
// Thumbnail
|
||||
public Thumbnail: string;
|
||||
|
||||
// What channel made this video.
|
||||
public Channel: string;
|
||||
|
||||
// Duration of the video in seconds.
|
||||
public Seconds: number;
|
||||
|
||||
// Tags relevant to the video.
|
||||
public Tags: Array<string>;
|
||||
|
||||
// Youtube URL of the video.
|
||||
public URL: string;
|
||||
|
||||
// Popuplate this using data from our server.
|
||||
constructor(v: any){
|
||||
if (v == null){
|
||||
this.Title = "NULL";
|
||||
this.Description = "NULL";
|
||||
this.ID = "NULL";
|
||||
this.Thumbnail = "NULL";
|
||||
this.Channel = "NULL";
|
||||
this.Seconds = 9999999;
|
||||
this.Tags = ["NULL", "NULL"];
|
||||
this.URL = "NULL";
|
||||
} else {
|
||||
this.Title = v.title;
|
||||
this.Description = v.description;
|
||||
this.ID = v.id;
|
||||
this.Thumbnail = v.thumbnail;
|
||||
this.Channel = v.channel;
|
||||
this.Seconds = v.seconds;
|
||||
this.Tags = v.tags;
|
||||
this.URL = "https://www.youtube.com/watch?v=" + v.id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Types of modifications which can be applied to various models.
|
||||
export enum Modification {
|
||||
Add, Delete, Refresh
|
||||
}
|
||||
|
||||
// Change a channel state.
|
||||
export async function channel_modify(youtubeID : string, modify : Modification): Promise<void | string> {
|
||||
switch (modify){
|
||||
case Modification.Add: {
|
||||
let URL = API_BASE_URL + '/Channels/' + youtubeID;
|
||||
let resp = await Axios.post(URL).catch((error) => {
|
||||
if (error.response)
|
||||
return error.response.data;
|
||||
else if (error.request)
|
||||
return error.request;
|
||||
else
|
||||
return "Axios request failed for unkown reason.";
|
||||
});
|
||||
if (typeof resp == "string")
|
||||
return resp;
|
||||
break;
|
||||
}
|
||||
case Modification.Delete: {
|
||||
let URL = API_BASE_URL + '/Channels/' + youtubeID;
|
||||
let resp = await Axios.delete(URL).catch(e => console.log(e));
|
||||
if (resp != null){
|
||||
if (resp.status != 200)
|
||||
return resp.data();
|
||||
else
|
||||
return;
|
||||
} else {
|
||||
return "Response is null";
|
||||
}
|
||||
}
|
||||
case Modification.Refresh: {
|
||||
let URL = API_BASE_URL + '/Channels/Update/' + youtubeID;
|
||||
let resp = await Axios.post(URL).catch(e => console.log(e));
|
||||
if (resp != null){
|
||||
if (resp.status != 200)
|
||||
return resp.data();
|
||||
else
|
||||
return;
|
||||
} else {
|
||||
return "Response is null";
|
||||
}
|
||||
}
|
||||
default: {
|
||||
console.log("Unknown request type, error ...");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Delete a video.
|
||||
export async function video_delete(youtubeID : string) : Promise<void> {
|
||||
let URL = API_BASE_URL + '/Videos/' + youtubeID;
|
||||
let resp = await Axios.delete(URL).catch(e => console.log(e));
|
||||
if ((resp == null) || (resp.data == null)){
|
||||
console.log("Video delete via " + URL + " FAIL");
|
||||
};
|
||||
}
|
||||
|
||||
// Search for channels based on a search string.
|
||||
export async function search_channels(searchstr : string): Promise<Array<Channel>> {
|
||||
// Temporary holder for data.
|
||||
let Channels : Array<Channel> = [];
|
||||
|
||||
// Ask server for data.
|
||||
let resp = await Axios.get(API_BASE_URL + '/Channels/' + searchstr).catch(e => console.log(e));
|
||||
|
||||
// Handle all our nulls.
|
||||
if (resp == null || resp.data == null)
|
||||
console.log("server /api/Channels/" + searchstr + " return is null");
|
||||
else {
|
||||
// Parse our videos.
|
||||
resp.data.forEach((c: any) => {
|
||||
// Trim description if needed.
|
||||
if (c.description.length > max_description_length) {
|
||||
c.description = c.description.substring(0, max_description_length) + " ...";
|
||||
}
|
||||
|
||||
// Add it to our array
|
||||
Channels.push(new Channel(c));
|
||||
});
|
||||
}
|
||||
|
||||
// Send back the resulting videos.
|
||||
return Promise.resolve(Channels);
|
||||
}
|
||||
|
||||
// Search for videos based on a search string.
|
||||
export async function search_videos(searchstr : string): Promise<Array<Video>> {
|
||||
// Temporary holder for videos.
|
||||
let Videos : Array<Video> = [];
|
||||
|
||||
// Ask server for videos.
|
||||
let resp = await Axios.get(API_BASE_URL + '/Videos/' + searchstr).catch(e => console.log(e));
|
||||
|
||||
// Handle all our nulls.
|
||||
if (resp == null || resp.data == null)
|
||||
console.log("server /videos/ return is null");
|
||||
else {
|
||||
// Parse our videos.
|
||||
resp.data.forEach((v: any) => {
|
||||
// Trim description if needed.
|
||||
if (v.description.length > max_description_length) {
|
||||
v.description = v.description.substring(0, max_description_length) + " ...";
|
||||
}
|
||||
|
||||
// Add it to our array
|
||||
Videos.push(new Video(v));
|
||||
});
|
||||
}
|
||||
|
||||
// Send back the resulting videos.
|
||||
return Promise.resolve(Videos);
|
||||
}
|
Reference in New Issue
Block a user