如何从 Instagram 的 media_preview 原始数据重新创建预览?

2022-01-17 00:00:00 instagram decode javascript html5-canvas

如果您从 Instagram 的 API 获取 JSON 数据,您会发现一个 media_preview 键,其值是一些 Base64 编码的数据.它真的看起来像一些非常小的预览二进制数据.可能被压缩了.

If you get JSON data from Instagram's API you will find a media_preview key, the value of which is some Base64-encoded data. It really looks like some very small preview binary data. Maybe compressed.

以这篇文章为例.它只是一个黑色方块,它的预览非常小.查看 它的 JSON:data['graphql']['media_preview'] = "ACoq5miiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKAP/Z"

Take this post for example. It's just a black square and its preview is very small. Look at its JSON: data['graphql']['media_preview'] = "ACoq5miiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKAP/Z"

我无法反转代码并找出 Instagram 是如何使用这些数据的.我应该怎么做才能从这些数据中创建预览?

I could not reverse the code and find out how Instagram uses this data. What should I do to create a preview from this data?

我不擅长理解缩小的 js,但我认为它会在动态创建的画布上为敏感内容"绘制模糊预览.

I am not good at understanding minified js, but I think it draws blurred preview for "sensitive content" on a dynamically created canvas.

这是一个帖子,以一些敏感内容(医疗)为例

Here is a post with some sensitive content (medical) as an example

更新:我注意到 b64decoded 数据总是以 00 2A 2A 开头并以 FF D9 结尾.

Update: I've noticed that b64decoded data always starts with 00 2A 2A and ends with FF D9.

推荐答案

我已经调试了几行代码,为你编译了这个函数.media_preview 部分数据非常小,因为在我的示例中,完整的 jpeg 标头已经在代码中可用,例如 jpegtpl:

I've debugged a few lines of code and compiled this function for you. media_preview part of data is so small because full jpeg header already available in code as jpegtpl in my example:

function ig_media_preview(base64data) {
	var jpegtpl = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEABsaGikdKUEmJkFCLy8vQkc/Pj4/R0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0cBHSkpNCY0PygoP0c/NT9HR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR//AABEIABQAKgMBIgACEQEDEQH/xAGiAAABBQEBAQEBAQAAAAAAAAAAAQIDBAUGBwgJCgsQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+gEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoLEQACAQIEBAMEBwUEBAABAncAAQIDEQQFITEGEkFRB2FxEyIygQgUQpGhscEJIzNS8BVictEKFiQ04SXxFxgZGiYnKCkqNTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqCg4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2dri4+Tl5ufo6ery8/T19vf4+fr/2gAMAwEAAhEDEQA/AA==",
		t = atob(base64data),
		p = t.slice(3).split(""),
		o = t.substring(0, 3).split("").map(function(e) {
			return e.charCodeAt(0)
		}),
		c = atob(jpegtpl).split("");
	c[162] = String.fromCharCode(o[1]);
	c[160] = String.fromCharCode(o[2]);
	return base64data ? "data:image/jpeg;base64," + btoa(c.concat(p).join("")) : null
};
      
var data = "ACoq5miiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKAP/Z";
var uri = ig_media_preview(data);
document.body.insertAdjacentHTML('beforeend', '<img src="' + uri + '">');

相关文章