node-ainzfb-new 1.6.2210-test → 1.6.2511-test

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. package/package.json +2 -2
  2. package/src/changeAvt.js +85 -0
  3. package/utils.js +111 -67
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-ainzfb-new",
3
- "version": "1.6.2210-test",
3
+ "version": "1.6.2511-test",
4
4
  "description": "A Facebook chat API that doesn't rely on XMPP. Will NOT be deprecated after April 30th 2015.",
5
5
  "scripts": {
6
6
  "test": "mocha",
@@ -35,7 +35,7 @@
35
35
  "https-proxy-agent": "latest",
36
36
  "is-hexcolor": "^1.0.0",
37
37
  "lodash": "",
38
- "mqtt": "^4.3.7",
38
+ "mqtt": "^3.0.0",
39
39
  "node-superfetch": "^0.2.3",
40
40
  "npmlog": "^1.2.0",
41
41
  "path": "latest",
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+
3
+ var utils = require("../utils");
4
+ var log = require("npmlog");
5
+ /**
6
+ * It posts an image to a Facebook profile
7
+ * @param Api - The API object
8
+ * @param BotID - The ID of the bot you want to post the image to.
9
+ * @param form - The form data that you want to send.
10
+ * @returns The JSON.parse(Data.split("for (;;);")[1]); is returning the following:
11
+ * {"__ar":1,"payload":null,"jsmods":{"require":[["ImageUploader","uploadPhoto",[{"__m":"__elem_0"},{"__m":"__elem_1"},{"__m":"__elem_2"},{"__m":"__
12
+ */
13
+ async function postImage(Api,BotID,form) {
14
+ var Data = await Api.httpPostFormData(`https://www.facebook.com/profile/picture/upload/?profile_id=${BotID}&photo_source=57&av=${BotID}`, form);
15
+ return JSON.parse(Data.split("for (;;);")[1]);
16
+ }
17
+
18
+ module.exports = function(defaultFuncs, api, ctx) {
19
+ /* Changing the profile picture of the bot. */
20
+ return function changeAvt(link, caption, callback) {
21
+ var resolveFunc = function() {};
22
+ var rejectFunc = function() {};
23
+ var returnPromise = new Promise(function(resolve, reject) {
24
+ resolveFunc = resolve;
25
+ rejectFunc = reject;
26
+ });
27
+
28
+ if (!callback) {
29
+ callback = function(err, data) {
30
+ if (err) return rejectFunc(err);
31
+ resolveFunc(data);
32
+ };
33
+ }
34
+ try {
35
+ var Fetch = require('axios')
36
+ Fetch.get(link, { responseType: "stream" }).then(data => {
37
+ postImage(api, ctx.userID, { file: data.data }).then(data => {
38
+ if (data.error) throw new Error({ error: data.error, des: data.error.errorDescription });
39
+ var form = {
40
+ av: ctx.userID,
41
+ fb_api_req_friendly_name: "ProfileCometProfilePictureSetMutation",
42
+ fb_api_caller_class: "RelayModern",
43
+ doc_id: "5066134240065849",
44
+ variables: JSON.stringify({
45
+ input: {
46
+ caption: (caption || ""),
47
+ existing_photo_id: data.payload.fbid,
48
+ expiration_time: null,
49
+ profile_id: ctx.userID,
50
+ profile_pic_method: "EXISTING",
51
+ profile_pic_source: "TIMELINE",
52
+ scaled_crop_rect: {
53
+ height: 1,
54
+ width: 1,
55
+ x: 0,
56
+ y: 0
57
+ },
58
+ skip_cropping: true,
59
+ actor_id: ctx.userID,
60
+ client_mutation_id: Math.round(Math.random() * 19).toString()
61
+ },
62
+ isPage: false,
63
+ isProfile: true,
64
+ scale: 3,
65
+ })
66
+ };
67
+ defaultFuncs
68
+ .post("https://www.facebook.com/api/graphql/", ctx.jar, form)
69
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
70
+ .then(function(resData) {
71
+ if (resData.error) throw resData;
72
+ else return callback(null,true)
73
+ })
74
+ .catch(function(err) {
75
+ return callback(err);
76
+ });
77
+ })
78
+ })
79
+ }
80
+ catch (e) {
81
+ throw e;
82
+ }
83
+ return returnPromise;
84
+ };
85
+ };
package/utils.js CHANGED
@@ -1019,75 +1019,119 @@ function makeDefaults(html, userID, ctx) {
1019
1019
  }
1020
1020
 
1021
1021
  function parseAndCheckLogin(ctx, defaultFuncs, retryCount) {
1022
- if (retryCount == undefined) retryCount = 0;
1023
- return function(data) {
1024
- return bluebird.try(function() {
1025
- log.verbose("parseAndCheckLogin", data.body);
1026
- if (data.statusCode >= 500 && data.statusCode < 600) {
1027
- if (retryCount >= 5) {
1028
- throw {
1029
- error: "Request retry failed. Check the `res` and `statusCode` property on this error.",
1030
- statusCode: data.statusCode,
1031
- res: data.body
1032
- };
1033
- }
1034
- retryCount++;
1035
- var retryTime = Math.floor(Math.random() * 5000);
1036
- log.warn("parseAndCheckLogin", "Got status code " + data.statusCode + " - " + retryCount + ". attempt to retry in " + retryTime + " milliseconds...");
1037
- var url = data.request.uri.protocol + "//" + data.request.uri.hostname + data.request.uri.pathname;
1038
- if (data.request.headers["Content-Type"].split(";")[0] === "multipart/form-data") {
1039
- return bluebird.delay(retryTime).then(() => defaultFuncs.postFormData(url, ctx.jar, data.request.formData, {}))
1040
- .then(parseAndCheckLogin(ctx, defaultFuncs, retryCount));
1041
- } else {
1042
- return bluebird.delay(retryTime).then(() => defaultFuncs.post(url, ctx.jar, data.request.formData))
1043
- .then(parseAndCheckLogin(ctx, defaultFuncs, retryCount));
1044
- }
1045
- }
1046
- if (data.statusCode !== 200) throw new Error("parseAndCheckLogin got status code: " + data.statusCode + ". Bailing out of trying to parse response.");
1047
-
1048
- var res = null;
1049
- try {
1050
- res = JSON.parse(makeParsable(data.body));
1051
- } catch (e) {
1052
- throw {
1053
- error: "JSON.parse error. Check the `detail` property on this error.",
1054
- detail: e,
1055
- res: data.body
1056
- };
1057
- }
1058
-
1059
- // In some cases the response contains only a redirect URL which should be followed
1060
- if (res.redirect && data.request.method === "GET") return defaultFuncs.get(res.redirect, ctx.jar).then(parseAndCheckLogin(ctx, defaultFuncs));
1061
-
1062
- // TODO: handle multiple cookies?
1063
- if (res.jsmods && res.jsmods.require && Array.isArray(res.jsmods.require[0]) && res.jsmods.require[0][0] === "Cookie") {
1064
- res.jsmods.require[0][3][0] = res.jsmods.require[0][3][0].replace("_js_", "");
1065
- var cookie = formatCookie(res.jsmods.require[0][3], "facebook");
1066
- var cookie2 = formatCookie(res.jsmods.require[0][3], "messenger");
1067
- ctx.jar.setCookie(cookie, "https://www.facebook.com");
1068
- ctx.jar.setCookie(cookie2, "https://www.messenger.com");
1069
- }
1070
-
1071
- // On every request we check if we got a DTSG and we mutate the context so that we use the latest
1072
- // one for the next requests.
1073
- if (res.jsmods && Array.isArray(res.jsmods.require)) {
1074
- var arr = res.jsmods.require;
1075
- for (var i in arr) {
1076
- if (arr[i][0] === "DTSG" && arr[i][1] === "setToken") {
1077
- ctx.fb_dtsg = arr[i][3][0];
1078
-
1079
- // Update ttstamp since that depends on fb_dtsg
1080
- ctx.ttstamp = "2";
1081
- for (var j = 0; j < ctx.fb_dtsg.length; j++) ctx.ttstamp += ctx.fb_dtsg.charCodeAt(j);
1082
- }
1083
- }
1022
+ if (retryCount == undefined) {
1023
+ retryCount = 0;
1024
+ }
1025
+ return function (data) {
1026
+ return bluebird.try(function () {
1027
+
1028
+ if (data.statusCode >= 500 && data.statusCode < 600) {
1029
+ if (retryCount >= 5) {
1030
+ throw {
1031
+ error:
1032
+ "Request retry failed. Check the `res` and `statusCode` property on this error.",
1033
+ statusCode: data.statusCode,
1034
+ res: data.body
1035
+ };
1036
+ }
1037
+ retryCount++;
1038
+ var retryTime = Math.floor(Math.random() * 5000);
1039
+ var url =
1040
+ data.request.uri.protocol +
1041
+ "//" +
1042
+ data.request.uri.hostname +
1043
+ data.request.uri.pathname;
1044
+ if (
1045
+ data.request.headers["Content-Type"].split(";")[0] ===
1046
+ "multipart/form-data"
1047
+ ) {
1048
+ return bluebird
1049
+ .delay(retryTime)
1050
+ .then(function () {
1051
+ return defaultFuncs.postFormData(
1052
+ url,
1053
+ ctx.jar,
1054
+ data.request.formData,
1055
+ {}
1056
+ );
1057
+ })
1058
+ .then(parseAndCheckLogin(ctx, defaultFuncs, retryCount));
1059
+ } else {
1060
+ return bluebird
1061
+ .delay(retryTime)
1062
+ .then(function () {
1063
+ return defaultFuncs.post(url, ctx.jar, data.request.formData);
1064
+ })
1065
+ .then(parseAndCheckLogin(ctx, defaultFuncs, retryCount));
1066
+ }
1067
+ }
1068
+ if (data.statusCode !== 200)
1069
+ throw new Error(
1070
+ "parseAndCheckLogin got status code: " +
1071
+ data.statusCode +
1072
+ ". Bailing out of trying to parse response."
1073
+ );
1074
+
1075
+ var res = null;
1076
+ try {
1077
+ res = JSON.parse(makeParsable(data.body));
1078
+ } catch (e) {
1079
+ throw {
1080
+ error: "JSON.parse error. Check the `detail` property on this error.",
1081
+ detail: e,
1082
+ res: data.body
1083
+ };
1084
+ }
1085
+
1086
+ // In some cases the response contains only a redirect URL which should be followed
1087
+ if (res.redirect && data.request.method === "GET") {
1088
+ return defaultFuncs
1089
+ .get(res.redirect, ctx.jar)
1090
+ .then(parseAndCheckLogin(ctx, defaultFuncs));
1091
+ }
1092
+
1093
+ // TODO: handle multiple cookies?
1094
+ if (
1095
+ res.jsmods &&
1096
+ res.jsmods.require &&
1097
+ Array.isArray(res.jsmods.require[0]) &&
1098
+ res.jsmods.require[0][0] === "Cookie"
1099
+ ) {
1100
+ res.jsmods.require[0][3][0] = res.jsmods.require[0][3][0].replace(
1101
+ "_js_",
1102
+ ""
1103
+ );
1104
+ var cookie = formatCookie(res.jsmods.require[0][3], "facebook");
1105
+ var cookie2 = formatCookie(res.jsmods.require[0][3], "messenger");
1106
+ ctx.jar.setCookie(cookie, "https://www.facebook.com");
1107
+ ctx.jar.setCookie(cookie2, "https://www.messenger.com");
1108
+ }
1109
+
1110
+ // On every request we check if we got a DTSG and we mutate the context so that we use the latest
1111
+ // one for the next requests.
1112
+ if (res.jsmods && Array.isArray(res.jsmods.require)) {
1113
+ var arr = res.jsmods.require;
1114
+ for (var i in arr) {
1115
+ if (arr[i][0] === "DTSG" && arr[i][1] === "setToken") {
1116
+ ctx.fb_dtsg = arr[i][3][0];
1117
+
1118
+ // Update ttstamp since that depends on fb_dtsg
1119
+ ctx.ttstamp = "2";
1120
+ for (var j = 0; j < ctx.fb_dtsg.length; j++) {
1121
+ ctx.ttstamp += ctx.fb_dtsg.charCodeAt(j);
1122
+ }
1084
1123
  }
1085
-
1086
- if (res.error === 1357001) throw { error: "Chưa Đăng Nhập Được - Appstate Đã Bị Lỗi" };
1087
- return res;
1088
- });
1124
+ }
1125
+ }
1126
+
1127
+ if (res.error === 1357001) {
1128
+ throw { error: "Not logged in." };
1129
+ }
1130
+ return res;
1131
+ });
1089
1132
  };
1090
- }
1133
+ }
1134
+
1091
1135
 
1092
1136
  function saveCookies(jar) {
1093
1137
  return function(res) {