Skip to content

Commit

Permalink
Rewrite Instagram Story, Fix #89
Browse files Browse the repository at this point in the history
  • Loading branch information
BochilGaming committed Oct 1, 2023
1 parent 145692a commit 785391d
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 131 deletions.
3 changes: 2 additions & 1 deletion packages/scraper-instagram/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as instagramdl } from './src/instagramdl-v1.js'
export { default as instagramdl } from './src/instagramdl-v1.js'
export { default as instagramStory } from './src/instagramstory-v1.js'
13 changes: 9 additions & 4 deletions packages/scraper-instagram/src/instagramdl-v1.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import got from 'got'
import cheerio from 'cheerio'
import { DEFAULT_HEADERS } from './constant.js'
import { InstagramdlArgsSchema, InstagramdlItem, InstagramdlSchema } from '../types/instagramdl-v1.js'
import {
Instagramdl,
InstagramdlArgsSchema,
InstagramdlSchema
} from '../types/instagramdl-v1.js'

export default async function instagramdl (url: string) {
InstagramdlArgsSchema.parse(arguments)
Expand All @@ -19,14 +23,15 @@ export default async function instagramdl (url: string) {
form
}).text()
const $ = cheerio.load(data)
const results: InstagramdlItem[] = []
const results: Instagramdl = []
$('.row > div').find('div.row').each(function () {
const $el = $(this)
const thumbnail = $el.find('img').attr('src')
const thumbnail = $el.find('img').attr('src')!
const url = 'https://www.w3toys.com/' + $el.find('a').attr('href')
results.push({
thumbnail,
url
url,
// TODO: Add type 'image' or 'video'
})
})
return InstagramdlSchema.parse(results)
Expand Down
43 changes: 43 additions & 0 deletions packages/scraper-instagram/src/instagramstory-v1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import got from 'got'
import cheerio from 'cheerio'
import { DEFAULT_HEADERS } from './constant.js'
import {
InstagramStory,
InstagramStoryArgsSchema,
InstagramStoryItem,
InstagramStorySchema
} from '../types/instagramstory-v1.js'

export default async function instagramStory (username: string) {
InstagramStoryArgsSchema.parse(arguments)

const form = {
url: `https://www.instagram.com/${username}/`,
lang_code: 'en',
token: ''
}
const data = await got.post('https://fastdl.app/c/', {
headers: {
...DEFAULT_HEADERS,
'content-type': 'application/x-www-form-urlencoded',
origin: 'https://fastdl.app',
'referer': 'https://fastdl.app/en/story-saver'
},
form
}).text()
const $ = cheerio.load(data)
const results: InstagramStory = []
$('div.max-w-md').each(function () {
const $el = $(this)
const thumbnail = $el.find('img.w-full').attr('src')!
const $a = $el.find('a')
const url = $a.attr('href')!
const type = $a.attr('data-mediatype')!.toLowerCase() as InstagramStoryItem['type']
results.push({
thumbnail,
url,
type
})
})
return InstagramStorySchema.parse(results)
}
1 change: 0 additions & 1 deletion packages/scraper-instagram/test/instagramdl-v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ test('Instagram Downloader', async (t) => {

let photos: Instagramdl
await t.test('Getting Metadata Photos', async () => {

photos = await instagramdl(IG_P_URL)
assert.strictEqual(photos.length > 5, true)
})
Expand Down
32 changes: 32 additions & 0 deletions packages/scraper-instagram/test/instagramstory-v1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { instagramStory } from '../index.js'
import { test } from 'node:test'
import assert from 'node:assert'
import got from 'got'
import { InstagramStory } from '../types/instagramstory-v1.js'

const USERNAME = 'raffinagita1717'

test('Instagram Story Downloader', async (t) => {
let stories: InstagramStory
await t.test('Getting Metadata', async () => {
stories = await instagramStory(USERNAME)
assert.strictEqual(stories.length > 0, true)
})

await t.test('Download Story', async () => {
if (!stories.length)
return t.skip('Test skipped -- error in getting metadata!')
const [{ url }] = stories
const buffer = await got(url).buffer()
assert.strictEqual(buffer.byteLength > 0, true)
})

await t.test('Download Story Thumbnail', async () => {
if (!stories.length)
return t.skip('Test skipped -- error in getting metadata!')
const [{ thumbnail }] = stories
const buffer = await got(thumbnail).buffer()
assert.strictEqual(buffer.byteLength > 0, true)
})

})
13 changes: 13 additions & 0 deletions packages/scraper-instagram/types/instagramstory-v1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { z } from 'zod'

export const InstagramStoryArgsSchema = z.object({
0: z.string()
})
export const InstagramStoryItemSchema = z.object({
thumbnail: z.string().url(),
url: z.string().url(),
type: z.literal('image').or(z.literal('video'))
})
export type InstagramStoryItem = z.infer<typeof InstagramStoryItemSchema>
export const InstagramStorySchema = z.array(InstagramStoryItemSchema)
export type InstagramStory = z.infer<typeof InstagramStorySchema>
2 changes: 0 additions & 2 deletions packages/scraper-sosmed/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { default as youtubeSearch } from './src/youtube-search.js'
export { default as aiovideodl } from './src/aiovideodl.js'
export { default as savefrom } from './src/savefrom.js'
export { default as snapsave } from './src/snapsave.js'
Expand All @@ -7,6 +6,5 @@ export { groupWA } from './src/groupWA.js'

export * from './src/facebook.js'
export * from './src/google-it.js'
export * from './src/instagram.js'
export * from './src/tiktok.js'
export * from './src/twitter.js'
103 changes: 0 additions & 103 deletions packages/scraper-sosmed/src/instagram.ts

This file was deleted.

20 changes: 0 additions & 20 deletions packages/scraper-sosmed/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { expect } from 'chai'
import {
googleIt,
tiktokdl,
instagramdl,
instagramStory,
facebookdl,
facebookdlv2,
twitterdl,
Expand All @@ -26,24 +24,6 @@ describe('Social media', () => {
})
})

describe('Instagram', function () {
this.timeout(15000)
it('Instagram downloader', (done) => {
// https://www.instagram.com/p/CaHpoweBjmx/?utm_source=ig_web_copy_link
instagramdl('https://www.instagram.com/reel/CxSEjxfyJtN/?utm_source=ig_web_copy_link').then(() => {

return done()
}).catch(done)
})

it('Instagram story', (done) => {
instagramStory('raffinagita1717').then(() => {

return done()
}).catch(done)
})
})

describe('Facebook (Metaverse :V)', function () {
this.timeout(10000)
it('Facebook downloader', done => {
Expand Down

0 comments on commit 785391d

Please sign in to comment.