First release
This commit is contained in:
45
src/lib/structures/Timeline.js
Normal file
45
src/lib/structures/Timeline.js
Normal file
@@ -0,0 +1,45 @@
|
||||
const constants = require("../constants")
|
||||
const TimelineImage = require("./TimelineImage")
|
||||
const collectors = require("../collectors")
|
||||
require("../testimports")(constants, TimelineImage)
|
||||
|
||||
function transformEdges(edges) {
|
||||
return edges.map(e => new TimelineImage(e.node))
|
||||
}
|
||||
|
||||
class Timeline {
|
||||
/**
|
||||
* @param {import("./User")} user
|
||||
*/
|
||||
constructor(user) {
|
||||
this.user = user
|
||||
this.pages = []
|
||||
this.addPage(this.user.data.edge_owner_to_timeline_media)
|
||||
this.page_info = this.user.data.edge_owner_to_timeline_media.page_info
|
||||
}
|
||||
|
||||
hasNextPage() {
|
||||
return this.page_info.has_next_page
|
||||
}
|
||||
|
||||
fetchNextPage() {
|
||||
if (!this.hasNextPage()) return constants.symbols.NO_MORE_PAGES
|
||||
return collectors.fetchTimelinePage(this.user.data.id, this.page_info.end_cursor).then(page => {
|
||||
this.addPage(page)
|
||||
return this.pages.slice(-1)[0]
|
||||
})
|
||||
}
|
||||
|
||||
async fetchUpToPage(index) {
|
||||
while (this.pages[index] === undefined && this.hasNextPage()) {
|
||||
await this.fetchNextPage()
|
||||
}
|
||||
}
|
||||
|
||||
addPage(page) {
|
||||
this.pages.push(transformEdges(page.edges))
|
||||
this.page_info = page.page_info
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Timeline
|
||||
41
src/lib/structures/TimelineImage.js
Normal file
41
src/lib/structures/TimelineImage.js
Normal file
@@ -0,0 +1,41 @@
|
||||
class GraphImage {
|
||||
/**
|
||||
* @param {import("../types").GraphImage} data
|
||||
*/
|
||||
constructor(data) {
|
||||
this.data = data
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} size
|
||||
*/
|
||||
getSuggestedThumbnail(size) {
|
||||
let found = null
|
||||
for (const tr of this.data.thumbnail_resources) {
|
||||
found = tr
|
||||
if (tr.config_width >= size) break
|
||||
}
|
||||
return found
|
||||
}
|
||||
|
||||
getSrcset() {
|
||||
return this.data.thumbnail_resources.map(tr => {
|
||||
const p = new URLSearchParams()
|
||||
p.set("width", String(tr.config_width))
|
||||
p.set("url", tr.src)
|
||||
return `/imageproxy?${p.toString()} ${tr.config_width}w`
|
||||
}).join(", ")
|
||||
}
|
||||
|
||||
getCaption() {
|
||||
if (this.data.edge_media_to_caption.edges[0]) return this.data.edge_media_to_caption.edges[0].node.text
|
||||
else return null
|
||||
}
|
||||
|
||||
getAlt() {
|
||||
// For some reason, pages 2+ don't contain a11y data. Instagram web client falls back to image caption.
|
||||
return this.data.accessibility_caption || this.getCaption() || "No image description available."
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = GraphImage
|
||||
21
src/lib/structures/User.js
Normal file
21
src/lib/structures/User.js
Normal file
@@ -0,0 +1,21 @@
|
||||
const Timeline = require("./Timeline")
|
||||
require("../testimports")(Timeline)
|
||||
|
||||
class User {
|
||||
/**
|
||||
* @param {import("../types").GraphUser} data
|
||||
*/
|
||||
constructor(data) {
|
||||
this.data = data
|
||||
this.following = data.edge_follow.count
|
||||
this.followedBy = data.edge_followed_by.count
|
||||
this.posts = data.edge_owner_to_timeline_media.count
|
||||
this.timeline = new Timeline(this)
|
||||
}
|
||||
|
||||
export() {
|
||||
return this.data
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = User
|
||||
3
src/lib/structures/index.js
Normal file
3
src/lib/structures/index.js
Normal file
@@ -0,0 +1,3 @@
|
||||
module.exports = {
|
||||
User: require("./User")
|
||||
}
|
||||
Reference in New Issue
Block a user