Skip to content

Commit

Permalink
CP-63 Implemented load post from Facebook with several images
Browse files Browse the repository at this point in the history
  • Loading branch information
hlushko committed Oct 11, 2018
1 parent 3a4df54 commit 1153435
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 28 deletions.
2 changes: 1 addition & 1 deletion chain-post/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,6 @@ <h5 class="modal-title" id="facebookTitle">Facebook post load</h5>
<script src="js/lib/jquery.min.js" integrity="sha384-tsQFqpEReu7ZLhBV2VZlAu7zcOV+rXbYlF2cqB8txI/8aZajjp4Bqd+V6D5IgvKT" crossorigin="anonymous"></script>
<script src="js/lib/bootstrap.bundle.min.js" integrity="sha384-pjaaA8dDz/5BgdFUPX6M/9SUZv4d12SUPF0axWc+VRZkx5xU3daN+lYb49+Ax+Tl" crossorigin="anonymous"></script>

<script src="js/main.min.js?t=1539272651"></script>
<script src="js/main.min.js?t=1539295753"></script>
</body>
</html>
128 changes: 102 additions & 26 deletions chain-post/js/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const htmlAccountsList = `accounts-list`

let sprintf = require(`sprintf-js`).sprintf
, jQuery = require(`jquery`)
, urlParse = require(`url-parse`)
, tool = require(`./tool`)
, Storage = require(`./storage`).Storage
, adapter = require(`./adapter`)
Expand Down Expand Up @@ -297,21 +298,40 @@ function setHandlerLoadFacebook() {
, facebookUrl = inputElement.val().replace(`www.facebook.com`, `m.facebook.com`)
, photoUrl = null
, buttonElement = jQuery(this).find(`.btn-primary`)
, onePhotoMode = false
, defaultTags = `facebook`
;

buttonElement.prop(constant.htmlNames.disabledPropName, true);

if (false === facebookUrl.startsWith(`https://m.facebook.com/photo.php`)) {
console.error(sprintf(`Only facebook photo supported, received: "%s".`, facebookUrl));
buttonElement.prop(constant.htmlNames.disabledPropName, false);
if (facebookUrl.startsWith(`https://m.facebook.com/photo.php`)) {
onePhotoMode = true;
}
if (facebookUrl.startsWith(`https://m.facebook.com/permalink.php`)) {
let parsed = urlParse(facebookUrl)
, queryParts = parsed.query.slice(1).split(`&`)
, queryParams = {}
, urlPattern = `https://m.facebook.com/%s/posts/pcb.%s/`
;

return false;
for (let i in queryParts) {
let [key, val] = queryParts[i].split(`=`);
queryParams[key] = decodeURIComponent(val);
}

facebookUrl = sprintf(urlPattern, queryParams.id, queryParams.story_fbid);
}
if (false === facebookUrl.includes(`locale=`)) {
facebookUrl += `&locale=ru_RU`;
let localeString = `locale=ru_RU`;
if (facebookUrl.endsWith(`/`)) {
localeString = `?` + localeString;
} else {
localeString = `&` + localeString;
}
facebookUrl += localeString;
}

function fbProcess(config) {
function fbOnePhotoProcess(config) {
// console.log(config);

let el = jQuery( `<div></div>` );
Expand All @@ -337,10 +357,64 @@ function setHandlerLoadFacebook() {
);
}

jQuery(constant.htmlNavigation.titleBlock).val(postTitle);
jQuery(constant.htmlNavigation.bodyBlock).val(postBody);
jQuery(constant.htmlNavigation.tagsBlock).val(`facebook`);
jQuery(constant.htmlNavigation.imagesBlock).val(JSON.stringify([photoUrl.replace(/&amp;/g, `&`)]));
fbFillSubmitFormAndCloseModal(
postTitle,
postBody,
defaultTags,
JSON.stringify([photoUrl.replace(/&amp;/g, `&`)])
);
}
function fbStoryProcess(content) {
let postTitle = ``
, postBody = ``
, postImages = ``
;

let textTitles = content.match(/"title":{"text":"(.+?)"/g)
, titles = []
;
for (let i in textTitles) {
let titleObj = JSON.parse(sprintf(`{%s}}`, textTitles[i]));

titles.push(titleObj.title.text);
}
if (titles.length > 1) {
postTitle = titles[1];
} else if (titles.length > 0) {
postTitle = titles[0];
}

let images = content.match(/"full_width_image":{"uri":"(.+?)"/g)
, imagesUrls = []
;
for (let i in images) {
let imageObj = JSON.parse(sprintf(`{%s}}`, images[i]));

imagesUrls.push(imageObj.full_width_image.uri);
}
if (imagesUrls.length > 0) {
postImages = JSON.stringify(imagesUrls);
}

let textBody = content.match(/"message":{"text":"(.+?)"/);
if (textBody && textBody.length > 1) {
let textObj = JSON.parse(sprintf(`{"body":"%s"}`, textBody[1]));

postBody = sprintf(
constant.htmlPieces.facebookPostBodyPattern,
textObj.body,
imagesUrls.join(`\n\n`),
facebookUrl.replace(`m.facebook.com`, `www.facebook.com`)
);
}

fbFillSubmitFormAndCloseModal(postTitle, postBody, defaultTags, postImages);
}
function fbFillSubmitFormAndCloseModal(title, body, tags, images) {
jQuery(constant.htmlNavigation.titleBlock).val(title);
jQuery(constant.htmlNavigation.bodyBlock).val(body);
jQuery(constant.htmlNavigation.tagsBlock).val(tags);
jQuery(constant.htmlNavigation.imagesBlock).val(images);

jQuery(`#facebookModal .close`).trigger(`click`);
inputElement.val(``);
Expand All @@ -350,22 +424,24 @@ function setHandlerLoadFacebook() {
jQuery.getJSON(
`https://allorigins.me/get?url=` + encodeURIComponent(facebookUrl) + `&callback=?`,
function(data) {
// console.log(data.contents);

let el = jQuery( `<div></div>` );
el.html(data.contents);

photoUrl = jQuery(`meta[property="og:image"]`, el).attr(`content`).replace(/&/g, `&amp;`);

let fbData = jQuery(`script:contains('require("MRenderingScheduler").getInstance().schedule({"id":"MPhotoContent"')`, el)
.text()
.replace(
`require("MRenderingScheduler").getInstance().schedule`,
`fbProcess`
)
;

eval(fbData);
if (onePhotoMode) {
let el = jQuery(`<div></div>`);
el.html(data.contents);

photoUrl = jQuery(`meta[property="og:image"]`, el).attr(`content`).replace(/&/g, `&amp;`);

let fbData = jQuery(`script:contains('require("MRenderingScheduler").getInstance().schedule({"id":"MPhotoContent"')`, el)
.text()
.replace(
`require("MRenderingScheduler").getInstance().schedule`,
`fbOnePhotoProcess`
)
;

eval(fbData);
} else {
fbStoryProcess(data.contents);
}
}
);
});
Expand Down
2 changes: 1 addition & 1 deletion chain-post/js/main.min.js

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions chain-post/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions chain-post/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"bootstrap": "^4.1.3",
"font-awesome": "^4.7.0",
"sprintf-js": "^1.1.1",
"url-parse": "^1.4.3",
"sleep-promise": "^8.0.1",
"fingerprintjs2sync": "^1.5.5",
"local-storage": "^1.4.2",
Expand Down

0 comments on commit 1153435

Please sign in to comment.