Skip to content

Commit 6f46ed8

Browse files
gooosemanScriptSmith
authored andcommitted
Fix getting only preloaded posts in full mode (#33)
* fix(user): fixes getting initial photos of a user, which has less then 10 photos * test: removes jest.setTimeout in new test * fix(instagram): moves this.finished check to getNext * fix(instagram): add responseFromAPI field
1 parent d8921ae commit 6f46ed8

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

src/api/instagram.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ export class Instagram<PostType> {
137137

138138
// Number of jumps before exiting because lack of data
139139
private failedJumps: number = 20;
140+
private responseFromAPI: boolean = false;
140141

141142
// Strings denoting the access methods of API objects
142143
private readonly pageQuery: string;
@@ -434,6 +435,9 @@ export class Instagram<PostType> {
434435
continue;
435436
}
436437

438+
// Acknowlege receipt of response
439+
this.responseFromAPI = true;
440+
437441
// Get JSON data
438442
let data: JSON;
439443
try {
@@ -609,18 +613,27 @@ export class Instagram<PostType> {
609613
await this.jump();
610614

611615
// Stop if no data is being gathered
612-
if (this.jumps === this.failedJumps && this.index === 0) {
613-
this.finished = true;
616+
if (this.jumps === this.failedJumps) {
617+
if (this.fullAPI) {
618+
if (!this.responseFromAPI) {
619+
this.finished = true;
620+
}
621+
} else if (this.index === 0) {
622+
this.finished = true;
614623

615-
const pageContent = {content: ""};
616-
try {
617-
pageContent.content = await this.page.content();
618-
} catch (e) {
619-
// No content
620-
}
624+
const pageContent = {content: ""};
625+
try {
626+
pageContent.content = await this.page.content();
627+
} catch (e) {
628+
// No content
629+
}
621630

622-
this.logger.error("Page failed to make requests", pageContent);
623-
break;
631+
this.logger.error(
632+
"Page failed to make requests",
633+
pageContent,
634+
);
635+
break;
636+
}
624637
}
625638

626639
// Enable grafting if required

tests/test.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,27 @@ describe("Full API", () => {
150150
}
151151
});
152152

153+
testWrapper("Account with < 10 photos", async () => {
154+
// This is a not well-known account and it can be deleted at any moment
155+
// If this test starts to fail, need to find another user
156+
// which has less then 10 photos
157+
const id = "zhiznizmelochei";
158+
const fullApiOption: IOptionsFullApi = {
159+
...libraryTestOptions,
160+
fullAPI: true,
161+
};
162+
const api = createApi("user", id, fullApiOption);
163+
const scraped = [];
164+
for await (const post of api.generator()) {
165+
expect(post).toBeDefined();
166+
scraped.push(post);
167+
}
168+
expect(scraped.length).toBeGreaterThan(0);
169+
// If this user will start to do new posts
170+
// Need to find a new one
171+
expect(scraped.length).toBeLessThan(10);
172+
});
173+
153174
describe("API limits", () => {
154175
class ApiTestConditions {
155176
public api: "hashtag" | "user";

0 commit comments

Comments
 (0)