Explorar el Código

Merge pull request #226 from singulart/master

#218 fixes and improvements
DzhideX hace 3 años
padre
commit
529ba0ed0c

+ 9 - 6
community-contributions/joystreamvideobot/src/bot.ts

@@ -3,6 +3,7 @@ import { readFileSync } from 'fs';
 import axios from 'axios';
 import {IVideoResponse, LooseObject}  from './types';
 
+import {humanFileSize} from './sizeformat';
 const moment = require('moment');
 const momentFormat = require("moment-duration-format");
 const Discord = require("discord.js");
@@ -54,23 +55,25 @@ const main = async () => {
                 .setColor('#4038FF') // official joystream blue, see https://www.joystream.org/brand/guides/
                 .setTitle(edge.node.title)
                 .setURL(`https://play.joystream.org/video/${edge.node.id}`)
-                .setDescription(edge.node.description.substring(1, 200)) // cut off lengthy descriptions 
+                .setDescription(edge.node.description.substring(0, 200)) // cut off lengthy descriptions 
                 .addFields(
                   { name: 'ID', value: edge.node.id, inline: true },
                   { name: 'Category', value: edge.node.category.name, inline: true},
                   { name: 'Duration', value: durationFormat(edge.node.duration), inline: true },
                   { name: 'Language', value: edge.node.language.iso, inline: true },
+                  { name: 'Size', value: humanFileSize(edge.node.mediaDataObject.size), inline: true },
                   { name: 'License', value: licenses[licenseKey], inline: true },
                 )
                 .setTimestamp();
-                if(edge.node.channel.avatarPhotoDataObject && edge.node.channel.avatarPhotoDataObject.liaison) {
+                const uploaderTitle = `${edge.node.channel.title} (${edge.node.channel.ownerMember.rootAccount})`
+                if(edge.node.channel.avatarPhotoDataObject?.liaison) {
                   const avatar = 
                         `${edge.node.channel.avatarPhotoDataObject.liaison.metadata}asset/v0/${edge.node.channel.avatarPhotoDataObject.joystreamContentId}`;
-                  exampleEmbed.setAuthor(edge.node.channel.title, avatar, `https://play.joystream.org/channel/${edge.node.channel.id}`);
+                  exampleEmbed.setAuthor(uploaderTitle, avatar, `https://play.joystream.org/channel/${edge.node.channel.id}`);
                 } else {
-                  exampleEmbed.setAuthor(edge.node.channel.title);
+                  exampleEmbed.setAuthor(uploaderTitle, null, `https://play.joystream.org/channel/${edge.node.channel.id}`);
                 }
-                if(edge.node.thumbnailPhotoDataObject && edge.node.thumbnailPhotoDataObject.liaison) {
+                if(edge.node.thumbnailPhotoDataObject?.liaison) {
                   exampleEmbed.setImage(`${edge.node.thumbnailPhotoDataObject.liaison.metadata}asset/v0/${edge.node.thumbnailPhotoDataObject.joystreamContentId}`)
                 }
               channel.send(exampleEmbed);
@@ -107,7 +110,7 @@ const cleanup = (ids: any[], cutoffDate: Date) => {
 
 const lookup = (ids: any[], id: string) => {
   for (let video of ids) {
-    if (video.id == id) {
+    if (video.id === id) {
       return true;
     }
   }

+ 32 - 0
community-contributions/joystreamvideobot/src/sizeformat.ts

@@ -0,0 +1,32 @@
+/**
+* Format bytes as human-readable text.
+* 
+* @param bytes Number of bytes.
+* @param si True to use metric (SI) units, aka powers of 1000. False to use 
+*           binary (IEC), aka powers of 1024.
+* @param dp Number of decimal places to display.
+* 
+* @return Formatted string.
+*/
+export const humanFileSize = (bytes: number, si=false, dp=1): string => {
+ const threshold = si ? 1000 : 1024;
+
+ if (Math.abs(bytes) < threshold) {
+   return bytes + ' B';
+ }
+
+ const units = si 
+   ? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] 
+   : ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
+ let u = -1;
+ const r = 10**dp;
+
+ do {
+   bytes /= threshold;
+   ++u;
+ } while (Math.round(Math.abs(bytes) * r) / r >= threshold && u < units.length - 1);
+
+
+ return bytes.toFixed(dp) + ' ' + units[u];
+ 
+}

+ 10 - 0
community-contributions/joystreamvideobot/src/types.ts

@@ -24,9 +24,14 @@ export interface INode {
     category: ICategory,
     language: ILanguage,
     license: ILicense,
+    mediaDataObject: IMediaDataObject,
     createdAt: string
 }
 
+export interface IMediaDataObject {
+    size: number
+}
+
 export interface ILicense {
     code: string
 }
@@ -43,10 +48,15 @@ export interface ILiaison {
 export interface IChannel {
     title: string,
     id: string,
+    ownerMember: IOwnerMember,
     avatarPhotoDataObject: IThumb,
     createdById: string;
 }
 
+export interface IOwnerMember {
+    rootAccount: string;
+}
+
 export interface ICategory {
     name: string;
 }

+ 3 - 0
community-contributions/joystreamvideobot/videos_query.graphql

@@ -84,6 +84,9 @@ fragment LicenseFields on License {
 fragment BasicChannelFields on Channel {
   id
   title
+  ownerMember {
+    rootAccount
+  }
   createdAt
   createdById
   avatarPhotoUrls