createVideo.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { ApiPromise, WsProvider } from '@polkadot/api'
  2. import { types as joyTypes } from '@joystream/types'
  3. import { Keyring } from '@polkadot/keyring'
  4. // Import input parser and video entity from @joystream/cd-schemas (we use it as library here)
  5. import { InputParser } from '@joystream/cd-schemas'
  6. import { VideoEntity } from '@joystream/cd-schemas/types/entities/VideoEntity'
  7. async function main() {
  8. // Initialize the api
  9. const provider = new WsProvider('ws://127.0.0.1:9944')
  10. const api = await ApiPromise.create({ provider, types: joyTypes })
  11. // Get Alice keypair
  12. const keyring = new Keyring()
  13. keyring.addFromUri('//Alice', undefined, 'sr25519')
  14. const [ALICE] = keyring.getPairs()
  15. const video: VideoEntity = {
  16. title: 'Example video',
  17. description: 'This is an example video',
  18. // We reference existing language and category by their unique properties with "existing" syntax
  19. // (those referenced here are part of inputs/entityBatches)
  20. language: { existing: { code: 'EN' } },
  21. category: { existing: { name: 'Education' } },
  22. // We use the same "existing" syntax to reference a channel by unique property (handle)
  23. // In this case it's a channel that we created in createChannel example
  24. channel: { existing: { handle: 'Example channel' } },
  25. media: {
  26. // We use "new" syntax to sygnalize we want to create a new VideoMedia entity that will be related to this Video entity
  27. new: {
  28. // We use "exisiting" enconding from inputs/entityBatches/VideoMediaEncodingBatch.json
  29. encoding: { existing: { name: 'H.263_MP4' } },
  30. pixelHeight: 600,
  31. pixelWidth: 800,
  32. // We create nested VideoMedia->MediaLocation->HttpMediaLocation relations using the "new" syntax
  33. location: { new: { httpMediaLocation: { new: { url: 'https://testnet.joystream.org/' } } } },
  34. },
  35. },
  36. // Here we use combined "new" and "existing" syntaxes to create Video->License->KnownLicense relations
  37. license: {
  38. new: {
  39. knownLicense: {
  40. // This license can be found in inputs/entityBatches/KnownLicenseBatch.json
  41. existing: { code: 'CC_BY' },
  42. },
  43. },
  44. },
  45. duration: 3600,
  46. thumbnailUrl: '',
  47. isExplicit: false,
  48. isPublic: true,
  49. }
  50. // Create the parser with known entity schemas (the ones in content-directory-schemas/inputs)
  51. const parser = InputParser.createWithKnownSchemas(
  52. api,
  53. // The second argument is an array of entity batches, following standard entity batch syntax ({ className, entries }):
  54. [
  55. {
  56. className: 'Video',
  57. entries: [video], // We could specify multiple entries here, but in this case we only need one
  58. },
  59. ]
  60. )
  61. // We parse the input into CreateEntity and AddSchemaSupportToEntity operations
  62. const operations = await parser.getEntityBatchOperations()
  63. await api.tx.contentDirectory
  64. .transaction(
  65. { Member: 0 }, // We use member with id 0 as actor (in this case we assume this is Alice)
  66. operations // We provide parsed operations as second argument
  67. )
  68. .signAndSend(ALICE)
  69. }
  70. main()
  71. .then(() => process.exit())
  72. .catch(console.error)