Import Instagram archive to a Bluesky account
9
fork

Configure Feed

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

Refactor media processing to handle single media and text truncation

- Improve handling of single media posts, especially for videos
- Add text truncation for long post titles
- Simplify media processing logic
- Optimize media type checking and embedding
- Update test logger to include debug method

+37 -38
+36 -38
src/media.ts
··· 87 87 : undefined; 88 88 let postText = post.title ?? ''; 89 89 90 - if (post.media?.length === 1) { 90 + if (postText.length > POST_TEXT_LIMIT) { 91 + postText = postText.substring( 92 + 0, 93 + POST_TEXT_LIMIT - POST_TEXT_TRUNCATE_SUFFIX.length 94 + ) + POST_TEXT_TRUNCATE_SUFFIX; 95 + } 96 + 97 + if (!post.media?.length) { 98 + return { 99 + postDate: postDate || null, 100 + postText, 101 + embeddedMedia: [], 102 + mediaCount: 0 103 + }; 104 + } 105 + 106 + if (post.media.length === 1) { 91 107 postText = postText || post.media[0].title; 92 108 postDate = postDate || new Date(post.media[0].creation_timestamp * 1000); 93 109 } ··· 95 111 let embeddedMedia: VideoEmbed | ImageEmbed[] = []; 96 112 let mediaCount = 0; 97 113 114 + // If first media is video, process only that 115 + const firstMedia = await processMedia(post.media[0], archiveFolder); 116 + if (firstMedia.isVideo) { 117 + if (firstMedia.mimeType && firstMedia.mediaBuffer && validateVideo(firstMedia.mediaBuffer)) { 118 + embeddedMedia = new VideoEmbedImpl( 119 + firstMedia.mediaText, 120 + firstMedia.mediaBuffer, 121 + firstMedia.mimeType 122 + ); 123 + mediaCount = 1; 124 + } 125 + return { postDate: postDate || null, postText, embeddedMedia, mediaCount }; 126 + } 127 + 128 + // Otherwise process images 98 129 for (let j = 0; j < post.media.length; j++) { 99 130 if (j >= MAX_IMAGES_PER_POST) { 100 131 logger.warn( ··· 107 138 post.media[j], 108 139 archiveFolder); 109 140 110 - if (!mimeType || !mediaBuffer) continue; 111 - 112 - if (isVideo && !validateVideo(mediaBuffer)) { 113 - continue; 114 - } 141 + if (!mimeType || !mediaBuffer || isVideo) continue; 115 142 116 - // Add media object for both simulate and real mode 117 - if (isVideo) { 118 - embeddedMedia = new VideoEmbedImpl( 119 - mediaText, 120 - mediaBuffer, 121 - mimeType 122 - ); 123 - } else { 124 - try{ 125 - if(Array.isArray(embeddedMedia)) { 126 - embeddedMedia.push( 127 - new ImageEmbedImpl(mediaText, mediaBuffer, mimeType) 128 - ); 129 - } else { 130 - logger.error('Embedded media is not an array!!!'); 131 - logger.debug('Embedded media present instead of an array?', embeddedMedia); 132 - } 133 - } catch (error) { 134 - logger.error('Failed to push image into embedded media', error); 135 - logger.debug('Embedded media present instead of an array?', embeddedMedia); 136 - throw error; 137 - } 138 - } 139 - 143 + (embeddedMedia as ImageEmbed[]).push( 144 + new ImageEmbedImpl(mediaText, mediaBuffer, mimeType) 145 + ); 140 146 mediaCount++; 141 - } 142 - 143 - if (postText.length > POST_TEXT_LIMIT) { 144 - postText = 145 - postText.substring( 146 - 0, 147 - POST_TEXT_LIMIT - POST_TEXT_TRUNCATE_SUFFIX.length 148 - ) + POST_TEXT_TRUNCATE_SUFFIX; 149 147 } 150 148 151 149 return {
+1
tests/media.test.ts
··· 13 13 error: jest.fn(), 14 14 warn: jest.fn(), 15 15 info: jest.fn(), 16 + debug: jest.fn(), 16 17 }, 17 18 })); 18 19