{"id":944,"date":"2014-12-07T13:16:29","date_gmt":"2014-12-07T13:16:29","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=944"},"modified":"2015-08-16T20:10:05","modified_gmt":"2015-08-16T20:10:05","slug":"how-to-get-all-of-your-own-facebook-posts-and-parse-them-with-javascript","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/javascript\/nodejs\/how-to-get-all-of-your-own-facebook-posts-and-parse-them-with-javascript\/","title":{"rendered":"How to get all of your own Facebook posts and parse them with JavaScript"},"content":{"rendered":"<p><!--My dad writes a lot of jokes (sorry, not in English :)) on his Facebook, so he wanted\u00a0me to pull all his posts from Facebook so that he can pick out easily few of the best ones in order to put them on his new\u00a0site where he'll be posting only jokes (site in development, will update with link when done).--><\/p>\n<p>So, first you need to download all the data that Facebook has stored for you (well, and what it&#8217;s willing to give you :)). You can do that by going to\u00a0<a href=\"https:\/\/www.facebook.com\/settings\">https:\/\/www.facebook.com\/settings<\/a>\u00a0when you&#8217;re logged in, and you will see the following screen:<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2014\/12\/FacebookDataDownload2.jpg\" rel=\"lightbox[944]\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-946 size-full\" src=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2014\/12\/FacebookDataDownload2.jpg\" alt=\"FacebookDataDownload2\" width=\"987\" height=\"407\" srcset=\"https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2014\/12\/FacebookDataDownload2.jpg 987w, https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2014\/12\/FacebookDataDownload2-300x123.jpg 300w\" sizes=\"auto, (max-width: 987px) 100vw, 987px\" \/><\/a><\/p>\n<p>Here you can just click on the Download a copy of your Facebook data and after they process your request you will get the download link on your email.<\/p>\n<p>The download package looks\u00a0contains 3 folders (videos, photos, html) and a index.html file. In order to take a look at your posts you have to open up the html folder where you&#8217;ll find the file named <strong>wall.htm<\/strong>. This is a simple HTML file whose contents\u00a0is put inside the\u00a0<strong>&lt;div class=&#8221;comment&#8221;&gt;\u00a0<\/strong>elements.<\/p>\n<p>In order to parse this with JavaScript (you&#8217;ll need to have Node.js installed) you can use the following snippet:<\/p>\n<pre class=\"lang:default decode:true \">function getMatches(string, regex, index) {\r\n    index || (index = 1); \/\/ default to the first capturing group\r\n    var matches = [];\r\n    var match;\r\n    while (match = regex.exec(string)) {\r\n        matches.push(match[index]);\r\n    }\r\n    return matches;\r\n}\r\n\r\nfs = require('fs');\r\nfs.readFile('wall.htm', 'utf8', function(err, data){\r\n\tif (err){\r\n\t    return console.log(err);\r\n\t}\r\n\r\n\tvar regex = \/comment\"&gt;([\\s\\S]*?)&lt;\\\/div&gt;\/g\r\n\tvar matches = getMatches(data, regex, 1);\r\n\r\n\tvar output = '';\r\n\tfor(var i=0; i&lt;matches.length; i++){\r\n\t\toutput += matches[i] + '\\n';\r\n\t}\r\n\r\n\tfs.writeFile('parsed.txt', output, 'utf8', function(){\r\n\t\tconsole.log('done!');\r\n\t});\r\n});<\/pre>\n<p>What this does is that it reads the wall.html file and outputs just simple text of the posts in a parsed.txt file.<\/p>\n<p>Few of the challenges here we&#8217;re the multiline regex matching which I solved with <a href=\"http:\/\/stackoverflow.com\/questions\/1068280\/javascript-regex-multiline-flag-doesnt-work\">this link<\/a> on StackOverflow, and another one was <a href=\"http:\/\/stackoverflow.com\/questions\/432493\/how-do-you-access-the-matched-groups-in-a-javascript-regular-expression\">getting access to\u00a0matched groups<\/a> of a regex in JavaScript.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, first you need to download all the data that Facebook has stored for you (well, and what it&#8217;s willing to give you :)). You can do that&hellip;<\/p>\n","protected":false},"author":1,"featured_media":948,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-944","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-nodejs"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/944","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=944"}],"version-history":[{"count":2,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/944\/revisions"}],"predecessor-version":[{"id":949,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/944\/revisions\/949"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/948"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}