get-media-change.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { WsProvider, ApiPromise } from "@polkadot/api";
  2. import { types } from "@joystream/types";
  3. import { Vec } from "@polkadot/types";
  4. import { EventRecord, Hash } from "@polkadot/types/interfaces";
  5. import { getChangeAction } from "./functions";
  6. async function main() {
  7. // Initialise the provider to connect to the local node
  8. const provider = new WsProvider('ws://127.0.0.1:9944');
  9. const api = await ApiPromise.create({ provider, types })
  10. const firstBlock = 1292265 // first block after the upgrade to the new Content Directory
  11. const lastBlock = 2332000
  12. // note that with this blockheight, you will see a lot of jsgenesis initialization and uploads
  13. for (let blockHeight=firstBlock; blockHeight<lastBlock; blockHeight++) {
  14. const blockHash = await api.rpc.chain.getBlockHash(blockHeight) as Hash
  15. const events = await api.query.system.events.at(blockHash) as Vec<EventRecord>;
  16. const eventsArray: EventRecord[] = []
  17. let eventIndex = 0
  18. for (let i=0; i<events.length; i++) {
  19. const section = events[i].event.section
  20. const method = events[i].event.method
  21. if(section == 'content') {
  22. console.log(`Event section=${section}, Event method=${method}`)
  23. eventsArray.push(events[i])
  24. if (method == "VideoCreated") {
  25. eventIndex+=1
  26. const cdChange = await getChangeAction(api, 'createVideo', blockHeight, blockHash, eventIndex, events[i])
  27. console.log("Change",JSON.stringify(cdChange, null, 4))
  28. }
  29. if (method == "VideoUpdated") {
  30. eventIndex+=1
  31. const cdChange = await getChangeAction(api, 'updateVideo', blockHeight, blockHash, eventIndex, events[i])
  32. console.log("Change",JSON.stringify(cdChange, null, 4))
  33. }
  34. if (method == "VideoDeleted") {
  35. eventIndex+=1
  36. const cdChange = await getChangeAction(api, 'deleteVideo', blockHeight, blockHash, eventIndex, events[i])
  37. console.log("Change",JSON.stringify(cdChange, null, 4))
  38. }
  39. if (method == "ChannelCreated") {
  40. eventIndex+=1
  41. const cdChange = await getChangeAction(api, 'createChannel', blockHeight, blockHash, eventIndex, events[i])
  42. console.log("Change",JSON.stringify(cdChange, null, 4))
  43. }
  44. if (method == "ChannelUpdated") {
  45. eventIndex+=1
  46. const cdChange = await getChangeAction(api, 'updateChannel', blockHeight, blockHash, eventIndex, events[i])
  47. console.log("Change",JSON.stringify(cdChange, null, 4))
  48. }
  49. }
  50. }
  51. }
  52. api.disconnect()
  53. }
  54. main()