Przeglądaj źródła

Merge pull request #528 from traumschule/featured-categories

Fix featured categories script
mochet 3 lat temu
rodzic
commit
b7522a8a34

+ 42 - 4
working-groups/curator-group/featured/README.md

@@ -1,12 +1,50 @@
 # [32.CC-2 - Update Featured Categories](https://blog.joystream.org/sumer-kpis/#32.CC-2)
 
-```
-yarn
-yarn run categories COMMAND
-```
+This script generates a week schedule to [configure featured categories on orion](https://github.com/Joystream/atlas/blob/master/docs/community/featured-content.md) ([atlas#1659](https://github.com/Joystream/atlas/issues/1659). To switch the categories it needs to be run daily.
+
+# Setup
+
+1. Run `yarn` to install dependencies.
+2. Edit `.env` to set `orionHeader`.
+3. Download the files shared by the Curation lead.
+
+# Usage
 
 Commands
 - `get`: show current category videos // TODO FIX
 - `set`: display mutation requests
 - `update`: updates videos.json from `list` (generate with `find DirectoryWithCategoryVideosCuts > list`
 - `schedule`: regenerates schedule
+
+`yarn run categories COMMAND`
+
+## Update Videos
+
+The list of video files is saved in `list` with this schema: `./News _ Politics 12/11-6571.mp4`
+
+To update it, download and enter the directory maintained by the Curation lead and run `find > list` and move the file `list` into the directory if the script.
+
+Now run `yarn categories update` to regenerate `videos.json`.
+
+## Update schedule
+
+`yarn categories schedule` saves the schedule for the coming week to `schedule.json`.
+
+## Change featured category videos
+
+With `yarn categories set` the videos of the day are loaded from `schedule.json` and transmitted to orion.
+
+## Verify categories
+
+Use `yarn categories get` to check currently set categories.
+
+
+# Configuration
+
+`.env` needs to hold the Authorization header shared by the atlas team (see [Setup](#setup)).
+
+To change the server, edit `orionUrl` in categories.ts.
+
+The location of video cuts is hardcoded in `getOrionVideo()`.
+
+A default of 7 days is used for the schedule (edit `generateSchedule()`).

+ 28 - 27
working-groups/curator-group/featured/categories.ts

@@ -99,59 +99,60 @@ const generateSchedule = (maxDays: number = 7): Schedule => {
 };
 
 const getCategoryFeaturedVideos = (): Promise<string> => {
-  //const headers = {};
-  const data =
-    '{ query: "query GetCategoriesFeaturedVideos {\n  allCategoriesFeaturedVideos {\n    categoryId\n    videos {\n      videoId\n      videoCutUrl\n    }\n  }\n}" }';
-  console.log(`sending`, data);
+  const data = {
+    query:
+      "query GetCategoriesFeaturedVideos {\n  allCategoriesFeaturedVideos {\n    categoryId\n    videos {\n      videoId\n      videoCutUrl\n    }\n  }\n}",
+  };
   return axios
     .post(orionUrl, data)
-    .then(({ data }: any) => JSON.stringify(data))
-    .catch((error: any) => error.message + JSON.stringify(error));
+    .then(({ data }: any) => {
+      fs.writeFileSync(`featured.json`, JSON.stringify(data));
+      console.log(`Wrote featured.json`);
+      return data;
+    })
+    .catch((error: any) => error.message + error);
 };
 
-const setCategoryVideos = (categoryId: number, videos: OrionVideo[]): string =>
-  `mutation {
-    setCategoryFeaturedVideos(
-        categoryId: "${categoryId}"
-        videos: ${JSON.stringify(videos)}
-    ) {
+const setCategoryVideos = (categoryId: number): string =>
+  `
+mutation SetFeaturedVideos ($videos: [FeaturedVideoInput!]!) {
+  setCategoryFeaturedVideos(categoryId: "${categoryId}", videos: $videos) {
         videoId
         videoCutUrl
-    }
-}`.replace(/\n/, "\n");
+  }
+}
+`;
 
 const setCategoryFeaturedVideos = async (
   categoryId: number,
   videos: OrionVideo[]
 ) => {
   const headers = { Authorization: orionHeader };
-  const data = setCategoryVideos(categoryId, videos);
-  return console.log(`request`, data); // TODO remove after fixing request
+  const query = setCategoryVideos(categoryId);
   axios
-    .post(orionUrl, { headers, data })
-    .then(async (res: any) => {
-      console.log(`sent post request to orion (${orionUrl})`, res);
-      //console.log(await getCategoryFeaturedVideos());
-    })
+    .post(orionUrl, { query, variables: { videos } }, { headers })
+    .then(async ({ status, statusText }: any) =>
+      console.log(`Set videos for category ${categoryId}:`, status, statusText)
+    )
     .catch((error: any) => {
       console.error(
-        `Failed to set featured videos for category ${categoryId}: ${JSON.stringify(
-          error
-        )}`
+        `Failed to set featured videos for category ${categoryId}:`,
+        error.response.data.errors.map(
+          (e: any) => e.message + JSON.stringify(e.locations)
+        )
       );
+      process.exit(1);
     });
 };
 
 const main = async (args: string[]) => {
   switch (args[0]) {
     case "get":
-      //const categoryId = Number(process.argv[2]);
-      console.log(await getCategoryFeaturedVideos());
+      getCategoryFeaturedVideos();
       break;
     case "set":
       try {
         const schedule: Schedule = require(scheduleFile);
-        //console.log(getDay(), Object.keys(schedule));
         if (!schedule || !schedule[getDay()]) {
           console.error(`Current day not found in schedule. Run update again.`);
           process.exit(1);