From 5c28f8dd48c18ff578f359d47fc4feee15195121 Mon Sep 17 00:00:00 2001 From: Isaac Abadi Date: Sat, 28 Nov 2020 22:04:28 -0500 Subject: [PATCH] Began work on allowing for more widespread usage of YT's API --- src/app/youtube.service.spec.ts | 16 +++++ src/app/youtube.service.ts | 116 ++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 src/app/youtube.service.spec.ts create mode 100644 src/app/youtube.service.ts diff --git a/src/app/youtube.service.spec.ts b/src/app/youtube.service.spec.ts new file mode 100644 index 0000000..df05ab5 --- /dev/null +++ b/src/app/youtube.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { YoutubeService } from './youtube.service'; + +describe('YoutubeService', () => { + let service: YoutubeService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(YoutubeService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/youtube.service.ts b/src/app/youtube.service.ts new file mode 100644 index 0000000..502af9a --- /dev/null +++ b/src/app/youtube.service.ts @@ -0,0 +1,116 @@ +import { Injectable } from '@angular/core'; +import { HttpClient } from '@angular/common/http'; +import { Observable } from 'rxjs'; + +export class Result { + id: string + title: string + desc: string + thumbnailUrl: string + videoUrl: string + uploaded: any; + + constructor(obj?: any) { + this.id = obj && obj.id || null + this.title = obj && obj.title || null + this.desc = obj && obj.desc || null + this.thumbnailUrl = obj && obj.thumbnailUrl || null + this.uploaded = obj && obj.uploaded || null + this.videoUrl = obj && obj.videoUrl || `https://www.youtube.com/watch?v=${this.id}` + + this.uploaded = formatDate(Date.parse(this.uploaded)); + } + +} + +@Injectable({ + providedIn: 'root' +}) +export class YoutubeService { + + base_url = 'https://www.googleapis.com/youtube/v3/'; + key = null; + + constructor(private http: HttpClient) { } + + initializeAPI(key) { + this.key = key; + } + + search(query: string): Observable { + const url_sub_path = 'search'; + + if (this.ValidURL(query)) { + return new Observable(); + } + const params: string = [ + `q=${query}`, + `key=${this.key}`, + `part=snippet`, + `type=video`, + `maxResults=5` + ].join('&') + const queryUrl = `${this.url}?${params}` + return this.http.get(queryUrl).map(response => { + return response['items'].map(item => { + return new Result({ + id: item.id.videoId, + title: item.snippet.title, + desc: item.snippet.description, + thumbnailUrl: item.snippet.thumbnails.high.url, + uploaded: item.snippet.publishedAt + }) + }) + }) + } + + getSubscribedChannels() { + const url_sub_path = '' + // on the first iteration, don't use a token. but because of the 50 channels limit, you need to use the returned token + // to retrieve the next list of 50 channels until a next token is not given + // https://stackoverflow.com/questions/52803732/youtube-api-v3-maximum-number-of-videos-only-50 + // https://developers.google.com/youtube/v3/docs/subscriptions/list?apix_params=%7B%22part%22%3A%5B%22snippet%2CcontentDetails%22%5D%2C%22maxResults%22%3A50%2C%22mine%22%3Atrue%2C%22pageToken%22%3A%22CGQQAA%22%7D + } + + getSubscribedChannelsWithToken() { + + } + + // checks if url is a valid URL + ValidURL(str) { + // tslint:disable-next-line: max-line-length + const strRegex = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/; + const re = new RegExp(strRegex); + return re.test(str); + } +} + +function formatDate(dateVal) { + const newDate = new Date(dateVal); + + const sMonth = padValue(newDate.getMonth() + 1); + const sDay = padValue(newDate.getDate()); + const sYear = newDate.getFullYear(); + let sHour: any; + sHour = newDate.getHours(); + const sMinute = padValue(newDate.getMinutes()); + let sAMPM = 'AM'; + + const iHourCheck = parseInt(sHour, 10); + + if (iHourCheck > 12) { + sAMPM = 'PM'; + sHour = iHourCheck - 12; + } else if (iHourCheck === 0) { + sHour = '12'; + } + + sHour = padValue(sHour); + + return sMonth + '-' + sDay + '-' + sYear + ' ' + sHour + ':' + sMinute + ' ' + sAMPM; +} + +function padValue(value) { + return (value < 10) ? '0' + value : value; +} +