media.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { ApiPromise } from "@polkadot/api";
  2. import { Vec, Option } from "@polkadot/types";
  3. import { Hash } from "@joystream/types/common";
  4. import { Entity } from "@joystream/types/content-directory";
  5. import { ContentId, DataObject } from "@joystream/types/media";
  6. import { getNextEntity, getEntity, getDataObject } from "./api";
  7. import { Channel, Media } from "./types";
  8. const CHANNEL_CLASS_iD = 1;
  9. const VIDEO_CLASS_iD = 10;
  10. const getEntities = async (
  11. api: ApiPromise,
  12. hash: Hash
  13. ): Promise<Map<number, Entity>> => {
  14. let nrEntities: number = await getNextEntity(api, hash);
  15. let entities = new Map<number, Entity>();
  16. for (let i = 0; i < nrEntities; ++i) {
  17. const entity: Entity = await getEntity(api, hash, i);
  18. entities.set(i, entity);
  19. }
  20. return entities;
  21. };
  22. export const computeUsedSpaceInMbs = async (
  23. api: ApiPromise,
  24. contentIds: Vec<ContentId>
  25. ): Promise<number> => {
  26. let space = 0;
  27. for (let contentId of contentIds) {
  28. const dataObject: Option<DataObject> = await getDataObject(api, contentId);
  29. space += dataObject.unwrap().size_in_bytes.toNumber();
  30. }
  31. return space / 1024 / 1024;
  32. };
  33. export const parseVideos = async (
  34. entities: Map<number, Entity>
  35. ): Promise<Media[]> => {
  36. let videos: Media[] = [];
  37. for (let [key, entity] of entities) {
  38. if (entity.class_id.toNumber() != VIDEO_CLASS_iD || entity.values.isEmpty)
  39. continue;
  40. let values = Array.from(entity.getField("values").entries());
  41. if (values.length < 2 || values[2].length < 1) continue;
  42. let title = values[2][1].getValue().toString();
  43. videos.push(new Media(key, title));
  44. }
  45. return videos;
  46. };
  47. const parseChannels = async (
  48. entities: Map<number, Entity>
  49. ): Promise<Channel[]> => {
  50. let channels: Channel[] = [];
  51. for (let [key, entity] of entities) {
  52. if (
  53. entity.class_id.toNumber() != CHANNEL_CLASS_iD ||
  54. entity.values.isEmpty
  55. ) {
  56. continue;
  57. }
  58. let values = Array.from(entity.getField("values").entries());
  59. let title = values[0][1].getValue().toString();
  60. channels.push(new Channel(key, title));
  61. }
  62. return channels;
  63. };