media.ts 1.6 KB

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