useYoutubeChannel.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { ref } from "vue";
  2. import Toast from "toasters";
  3. import { useWebsocketsStore } from "@/stores/websockets";
  4. // type YoutubeChannel = {
  5. // _id: string;
  6. // channelId: string;
  7. // title: string;
  8. // customUrl?: string;
  9. // rawData: {
  10. // kind: string;
  11. // etag: string;
  12. // id: string;
  13. // snippet: {
  14. // title: string;
  15. // description: string;
  16. // customUrl?: string;
  17. // publishedAt: string;
  18. // thumbnails: {
  19. // default: {
  20. // url: string;
  21. // width: number;
  22. // height: number;
  23. // };
  24. // medium: {
  25. // url: string;
  26. // width: number;
  27. // height: number;
  28. // };
  29. // high: {
  30. // url: string;
  31. // width: number;
  32. // height: number;
  33. // };
  34. // };
  35. // defaultLanguage?: string;
  36. // localized: {
  37. // title: string;
  38. // description: string;
  39. // };
  40. // };
  41. // contentDetails: {
  42. // relatedPlaylists: {
  43. // likes: string;
  44. // uploads: string;
  45. // };
  46. // };
  47. // statistics: {
  48. // viewCount: string;
  49. // subscriberCount: string;
  50. // hiddenSubscriberCount: boolean;
  51. // videoCount: string;
  52. // };
  53. // topicDetails: {
  54. // topicIds: string[];
  55. // topicCategories: string[];
  56. // };
  57. // status: {
  58. // privacyStatus: string;
  59. // isLinked: boolean;
  60. // longUploadsStatus: string;
  61. // madeForKids?: boolean;
  62. // };
  63. // brandingSettings: {
  64. // channel: {
  65. // title: string;
  66. // keywords?: string;
  67. // description?: string;
  68. // unsubscribedTrailer?: string;
  69. // defaultLanguage?: string;
  70. // };
  71. // image?: {
  72. // bannerExternalUrl: string;
  73. // };
  74. // };
  75. // };
  76. // documentVersion: number;
  77. // createdAt: Date;
  78. // updatedAt: Date;
  79. // };
  80. const youtubeChannelIDRegex = /(?<youtubeChannelID>UC[A-Za-z0-9-_]+)/;
  81. export const useYoutubeChannel = () => {
  82. const youtubeChannelURLOrID = ref("");
  83. const { socket } = useWebsocketsStore();
  84. const getYoutubeChannel = async () => {
  85. const youtubeChannelURLOrIDTrimmed = youtubeChannelURLOrID.value.trim();
  86. if (youtubeChannelURLOrIDTrimmed.length === 0)
  87. return new Toast(
  88. "No YouTube channel ID found in the supplied value."
  89. );
  90. const youtubeChannelIDRegexResult = youtubeChannelIDRegex.exec(
  91. youtubeChannelURLOrIDTrimmed
  92. );
  93. if (!youtubeChannelIDRegexResult)
  94. return new Toast(
  95. "No YouTube channel ID found in the supplied value."
  96. );
  97. return new Promise((resolve, reject) => {
  98. socket.dispatch(
  99. "youtube.getChannel",
  100. youtubeChannelIDRegexResult.groups.youtubeChannelID,
  101. res => {
  102. if (res.status === "success") {
  103. const { data } = res;
  104. resolve(data);
  105. youtubeChannelURLOrID.value = "";
  106. } else if (res.status === "error") {
  107. new Toast(res.message);
  108. reject();
  109. }
  110. }
  111. );
  112. });
  113. };
  114. const getYoutubeChannelVideos = async channelId =>
  115. new Promise((resolve, reject) => {
  116. socket.dispatch("youtube.getChannelVideos", channelId, res => {
  117. if (res.status === "success") {
  118. const { data } = res;
  119. resolve(data);
  120. } else if (res.status === "error") {
  121. new Toast(res.message);
  122. reject();
  123. }
  124. });
  125. });
  126. return {
  127. youtubeChannelURLOrID,
  128. getYoutubeChannel,
  129. getYoutubeChannelVideos
  130. };
  131. };