Facebook 照片上传日期时间戳

Facebook photo upload date timestamp

我已经下载了我所有的 Facebook 数据,并希望将我通过 Messenger 发送的一些图像上传到 Google 照片。我希望他们拥有正确的元数据,以便在正确的日期上传,而不是 today。不幸的是,他们有 Date created.

的下载日期

我尝试解析标题,但它似乎不是时间戳。

我的问题是:有没有办法创建一个脚本,将正确的元数据添加到从 Facebook 下载的照片(通过 Download your information 存档)?一个示例标题是:142666616_209126620919024_535058535265435125_n.jpg。这张照片的日期应该是 Jan 27, 2021, 10:53 AM.

经过一番挖掘,我找到了解决方案。

Facebook 为您提供的存档具有以下结构的每个朋友的文件夹:

\friend_name_a1b2c3
   \photos
      12345678_123456788996_123124421.jpg
   \gifs
   \audio
    messages_1.json

messages_1.json 有你与那个朋友的所有消息,这里是消息的示例:

{
    "sender_name": "Your Name",
    "timestamp_ms": 1562647443588,
    "photos": [
      {
        "uri": "messages/inbox/friend_name_a1b2c3/photos/12345678_123456788996_123124421.jpg",
        "creation_timestamp": 1562647443
      }
    ],
    "type": "Generic",
    "is_unsent": false
},

因此,使用 glob and utimes 我想出了以下脚本:

var glob = require("glob")
var Promise = require('bluebird');
var fs = Promise.promisifyAll(require("fs"));
var { utimes } = require("utimes");

const readJSONFiles = async () => {
  const messagesFiles = glob.sync(`**/message_*.json`)
  const promises = [];
  messagesFiles.forEach(mFile => {
    promises.push(fs.readFileAsync(mFile, 'utf8'));
  })

  return Promise.all(promises);
}

readJSONFiles().then(result => {
  const map = {};
  result.forEach(data => {
    const messagesContents = JSON.parse(data);
    messagesContents.messages
      .filter(m => m.photos)
      .forEach(m => {
        m.photos.filter(p => {
          const splitted = p.uri.split("/")
          const messagePhotoFileName = splitted[splitted.length - 1];
          map[messagePhotoFileName] = m.timestamp_ms;
        })
      })
  })

  fs.writeFileSync("./map.json", JSON.stringify(map))
}).then(() => {
  fs.readFileAsync("./map.json", 'utf8').then(data => {
    const map = JSON.parse(data);
    glob("**/*.jpg", function (er, files) {
      files.forEach(file => {
        const [, , photo] = file.split("/");

        utimes(file, {
          btime: map[photo],
          atime: map[photo],
          mtime: map[photo]
        });
      })
    })
  })
});

它创建一个 file-name:date-taken 的映射,然后遍历所有 .jpg 文件并更改其元数据。它的边缘肯定有点粗糙,但毕竟完成了工作。