Import Instagram archive to a Bluesky account
9
fork

Configure Feed

Select the types of activity you want to include in your feed.

Refactor Instagram media processing to improve async handling and media processing

- Update process method to handle media processing asynchronously
- Use Promise.all to resolve all media processing promises
- Improve type safety by returning Promise<ProcessedPost[]>
- Add detailed documentation for the processing method
- Simplify media processing logic with more consistent promise handling

+43 -15
+43 -15
src/media/media.ts
··· 70 70 public archiveFolder: string 71 71 ) {} 72 72 73 + /** 74 + * Processes Instagram posts and their associated media into a format 75 + * that can be easily mapped to Bluesky's requirements. 76 + * 77 + * This method iterates over each Instagram post, processes the media 78 + * (either images or videos), and returns a Promise that resolves to 79 + * an array of ProcessedPost objects once all media processing is complete. 80 + * 81 + * @returns {Promise<ProcessedPost[]>} A promise that resolves to an array of ProcessedPost objects. 82 + */ 73 83 public process(): Promise<ProcessedPost[]> { 74 - const processingPosts: ProcessedPost[] = []; 75 - for(const post of this.instagramPosts){ 84 + // Array to hold promises for each processed post 85 + const processingPosts: Promise<ProcessedPost>[] = []; 86 + 87 + // Iterate over each Instagram post 88 + for (const post of this.instagramPosts) { 89 + // Create a new date object from the post's creation timestamp 76 90 const postDate = new Date(post.creation_timestamp * 1000); 91 + 92 + // Initialize a new ProcessedPostImpl object with the post date and title 77 93 const processingPost = new ProcessedPostImpl(postDate, post.title); 78 - let processingMedia: Promise<MediaProcessResult | MediaProcessResult[]>; 79 - // If its an array we know we have many images. 80 - // Does not support images and videos in the same post at this time. 81 - if(Array.isArray(post.media)) { 82 - const imageProcessor = new InstagramImageProcessor(post.media, this.archiveFolder); 94 + 95 + // Declare a promise to hold the processing result for the media 96 + let processingMedia: Promise<ProcessedPost>; 97 + 98 + // Check if the post contains multiple media items (images) 99 + if (Array.isArray(post.media)) { 100 + // Create an image processor for the array of images 101 + const imageProcessor = new InstagramImageProcessor(post.media, this.archiveFolder); 102 + 103 + // Process the images and update the processingPost with the results 83 104 const processingImages: Promise<MediaProcessResult[]> = imageProcessor.process(); 84 - processingImages.then((processedImages) => processingPost.embeddedMedia = processedImages); 85 - processingMedia = processingImages; 105 + processingMedia = processingImages.then((processedImages) => { 106 + processingPost.embeddedMedia = processedImages; // Set the embedded media 107 + return processingPost; // Return the processed post 108 + }); 86 109 } else { 87 - // If its not an array we know we have a single video post 88 - // A strong assumption that should be changed once both medias are handled the same. 110 + // If the post contains a single video 89 111 const videoProcessor = new InstagramVideoProcessor(post.media as VideoMedia, this.archiveFolder); 112 + 113 + // Process the video and update the processingPost with the result 90 114 const processingVideo: Promise<MediaProcessResult> = videoProcessor.process(); 91 - processingVideo.then((processedVideo)=> processingPost.embeddedMedia = processedVideo); 92 - processingMedia = processingVideo; 115 + processingMedia = processingVideo.then((processedVideo) => { 116 + processingPost.embeddedMedia = processedVideo; // Set the embedded media 117 + return processingPost; // Return the processed post 118 + }); 93 119 } 94 120 95 - processingPosts.push(processingPost); 121 + // Add the processing promise to the array 122 + processingPosts.push(processingMedia); 96 123 } 97 124 98 - return Promise.resolve(processingPosts); 125 + // Wait for all processing promises to resolve and return the final array of processed posts 126 + return Promise.all(processingPosts); 99 127 } 100 128 } 101 129