Skip to content

Commit

Permalink
[Update] new structure + bookmarks + basic menu
Browse files Browse the repository at this point in the history
  • Loading branch information
HoangTran0410 committed Sep 17, 2021
1 parent e1b3acf commit 255f075
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 22 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ Kết quả sẽ xuất trong màn hình console

## BOOKMARK HỖ TRỢ

- <a href='javascript: (function () {if(window.location.host==="m.facebook.com"){console.log("Đang lấy token ...");fetch("https://m.facebook.com/composer/ocelot/async_loader/?publisher=feed").then((response)=>response.text()).then((text)=>{if("<"==text[0]){alert("Chưa đăng nhập. Bạn cần đăng nhập fb thì mới lấy được token.")}else{window.prompt("Token:",/(?<=accessToken\\":\\")(.*?)(?=\\")/.exec(text)[0])}})}else{alert("Bookmark này chỉ hoạt động trên trang m.facebook.com\nBạn hãy vào trang trên và ấn lại bookmark để lấy token an toàn nhé.");window.open("https://m.facebook.com")}})()'>Bookmark</a>
giúp lấy Access Token FB AN TOÀN ([Soruce Code](./more/get_fb_token_bookmark.js))
Mình cũng sẵn tiện viết 1 vài đoạn bookmark script. Các bạn có thể vào [bookmarks.js](./scripts/bookmarks.js) để lấy.

- Bookmark lấy **Access Token**
- Bookmark lấy **Group ID** - trường hợp url của group hiển thị tên chứ ko hiển thị id

## LINK NGON

Expand Down
7 changes: 4 additions & 3 deletions config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// do not modify this
export const FB_API_HOST = "https://graph.facebook.com";
export const FB_API_HOST = "https://graph.facebook.com/v12.0";

// you can modify all the variables below
export const ACCESS_TOKEN = "YOUR_ACCESS_TOKEN";
export const WAIT_BEFORE_NEXT_FETCH = 0;
export const ID_LINK_SEPERATOR = ";";
export const FOLDER_TO_SAVE_LINKS = "links";
export const FOLDER_TO_SAVE_IMAGES = 'images';
export const PHOTO_FILE_FORMAT = 'png'; // OR jpg
export const FOLDER_TO_SAVE_LINKS = "downloads/links";
export const FOLDER_TO_SAVE_IMAGES = 'downloads/images';
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file removed images/245004546697321/360758271615678.png
Binary file not shown.
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import {
fetchAlbumInfo,
saveAlbumPhoto,
saveAlbumPhotoLinks,
} from "./dowload_album.js";
} from "./scripts/download_album.js";
import {
fetchTimeLineAlbumId,
saveTimeLineAlbum_FBPage,
saveTimeLineAlbumPhotoLinks_FBPage,
} from "./download_timeline_album.js";
} from "./scripts/download_timeline_album.js";

// import { menu } from "./scripts/menu.js";
// menu();

// ============ Lấy thông tin album timeline từ page id trên fb ============
(async () => {
Expand Down
16 changes: 14 additions & 2 deletions more/get_fb_token_bookmark.js → scripts/bookmarks.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
// Lấy group id - trường hợp url của group hiển thị tên chứ ko hiển thị id
javascript: (function () {
const list_a = document.querySelectorAll("a");
for (let a of Array.from(list_a)) {
const found = /(?<=\/groups\/)(.+\d)(?=\/user\/)/.exec(a);
if (found && found[0]) {
window.prompt("GROUP ID:", found[0]);
return;
}
}
alert("Group id Not found!");
})();

// Chỉ dùng cho trang m.facebook.com
// Lấy access token - Chỉ dùng cho trang m.facebook.com
javascript: (function () {
if (window.location.host === "m.facebook.com") {
console.log("Đang lấy token ...");
Expand All @@ -23,7 +35,7 @@ javascript: (function () {
}
})();

// Dùng cho www.facebook.com - script chưa được kiểm định an toàn
// Lấy access token Dùng cho www.facebook.com - script chưa được kiểm định an toàn
javascript: (function () {
var uid = document.cookie.match(/c_user=(d+)/)[1],
dtsg = document.getElementsByName("fb_dtsg")[0].value,
Expand Down
16 changes: 6 additions & 10 deletions dowload_album.js → scripts/download_album.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
ID_LINK_SEPERATOR,
FOLDER_TO_SAVE_LINKS,
FOLDER_TO_SAVE_IMAGES,
} from "./config.js";
PHOTO_FILE_FORMAT,
} from "../config.js";
import {
createIfNotExistDir,
deleteFile,
Expand Down Expand Up @@ -98,26 +99,21 @@ const fetchAlbumPhotos = async ({
// Bạn có thể thêm những trường khác vào url để lấy được nhiều thông tin hơn, tìm hiểu các trường trong https://developers.facebook.com/tools/explorer/
export const fetchAlbumInfo = async (albumId) => {
// create link to fetch
let url = `${FB_API_HOST}/${albumId}?fields=count,link,name,updated_time,id,created_time,type,from&access_token=${ACCESS_TOKEN}`;
let url = `${FB_API_HOST}/${albumId}?fields=count,link,name&access_token=${ACCESS_TOKEN}`;

try {
// fetch data
const response = await fetch(url);
const json = await response.json();

if(json.error) throw json.error;

// return album infomation
return {
id: albumId,
type: json.type,
count: json.count,
link: json.link,
name: json.name,
created_time: json.created_time,
updated_time: json.updated_time,
from: {
name: json.from?.name,
id: json.from?.id,
},
};
} catch (e) {
console.error("ERROR while fetch album information", e);
Expand Down Expand Up @@ -159,7 +155,7 @@ export const saveAlbumPhoto = async (albumId) => {
const photo_id = seperated[0];
const link = seperated[1];

const savePath = `${dir}/${photo_id}.png`;
const savePath = `${dir}/${photo_id}.${PHOTO_FILE_FORMAT}`;
promises.push(
downloadFileSync({
uri: link,
Expand Down
20 changes: 20 additions & 0 deletions scripts/download_groupfeeds_media.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import fetch from "node-fetch";
import { ACCESS_TOKEN, FB_API_HOST } from "../config.js";

export const fetchGroupFeeds = async (groupId) => {
const fetchOnce = async (_url) => {
try {
const response = await fetch(_url);
const json = await response.json();
return json;
} catch (e) {
return {};
}
};

const url = `${FB_API_HOST}/${groupId}/feed?fields=attachments&access_token=${ACCESS_TOKEN}`;
const firstFetch = await fetchOnce(url);

};

fetchGroupFeeds(697332711026460);
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// album này sẽ không hiện trên trang web facebook (hoặc có mà mình ko biết cách tìm ở đâu), cần dùng tool để lấy

import fetch from "node-fetch";
import { ACCESS_TOKEN, FB_API_HOST } from "./config.js";
import { saveAlbumPhoto, saveAlbumPhotoLinks } from "./dowload_album.js";
import { ACCESS_TOKEN, FB_API_HOST } from "../config.js";
import { saveAlbumPhoto, saveAlbumPhotoLinks } from "./download_album.js";

export const fetchTimeLineAlbumId = async (page_id) => {
// create link to fetch all albums of page
Expand All @@ -14,7 +14,7 @@ export const fetchTimeLineAlbumId = async (page_id) => {
// fetch data
const response = await fetch(url);
const json = await response.json();

// find timeline album
const timeLineAlbum = json.data.find((_) => _.type === "wall");

Expand Down
63 changes: 63 additions & 0 deletions scripts/menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import readline from "readline";
import {
fetchAlbumInfo,
saveAlbumPhoto,
saveAlbumPhotoLinks,
} from "./download_album.js";
import {
fetchTimeLineAlbumId,
saveTimeLineAlbum_FBPage,
saveTimeLineAlbumPhotoLinks_FBPage,
} from "./download_timeline_album.js";

// https://stackoverflow.com/a/68504470
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const prompt = (query) => new Promise((resolve) => rl.question(query, resolve));
rl.on("close", () => process.exit(0));

const wait_input = async () => await prompt("");

const choose = async (title, menu_items) => {
let ui = `\n======== ${title} ========`;
Object.entries(menu_items).map(([key, value], index) => {
ui += `\n${key}: ${value}`;
});
console.log(ui);

while (true) {
const input = await prompt("> Choose action: ");
if (input in menu_items) {
return {
key: input,
value: menu_items[input],
};
} else {
console.log("[!] Invalid. Please choose again.");
}
}
};

// ========================================== MENU =========================================

export const menu = async () => {
while (true) {
const main_menu = {
0: "Download Album",
3: "Get Album Infomation",
4: "Exit",
};

const action = await choose("FB Album Downloader Tool", main_menu);
if (action.key == 2) {
const album_id = await prompt("Enter album id: ");
const album_info = await fetchAlbumInfo(album_id);
console.log(album_info);
}
if (action.key == 3) break;
}

rl.close();
};
File renamed without changes.
File renamed without changes.

0 comments on commit 255f075

Please sign in to comment.