185 lines
5.8 KiB
TypeScript
185 lines
5.8 KiB
TypeScript
import Axios from "axios";
|
|
|
|
export namespace API {
|
|
// Base URL for API queries.
|
|
export let Base_URL = "/api";
|
|
|
|
// 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;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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 and add it to the video output.
|
|
resp.data.forEach((c: any) => 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) => Videos.push(new Video(v)));
|
|
}
|
|
|
|
// Send back the resulting videos.
|
|
return Promise.resolve(Videos);
|
|
}
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
}
|
|
}
|