[InstagramAPI] 投稿を取得してHTMLで表示

1. facebookとinstagramのアカウントを開設(すでにある場合はスキップ)
2. instagramをビジネスアカウントへ変更
3. Facebookページの作成
4. FacebookページとInstagramビジネスアカウントの紐づけ
5. Facebookアプリ作成
6. アクセストークンを取得する ()
– InstagramグラフAPIエクスプローラーで、アクセストークンを取得
instagram_basic
instagram_manage_comments
instagram_manage_insights
instagram_manage_messages
instagram_content_publish
business_management
– 上記で取得した「アクセストークン」と、Facebookアプリの「アプリID」と「app secret」からアクセストークンを取得
https://graph.facebook.com/v4.0/oauth/access_token?grant_type=fb_exchange_token&client_id=[★アプリID]&client_secret=[★app secret]&fb_exchange_token=[★1番目のトークン]

– https://graph.facebook.com/v4.0/me?access_token=[★2番目のトークン] でIDを取得
– https://graph.facebook.com/v4.0/[★ここにIDを入力]/accounts?access_token=[★2番目のトークン]
– InstagramビジネスアカウントIDを取得

### htmlで表示
conf.php

<?php

return [
    'business_id' => '${business_id}',
    'access_token' => '${access_token}',
];
?>

index.php

<?php

$config = include(__DIR__ . '/conf.php');

$business_id = $config['business_id'];
$access_token = $config['access_token'];

if(isset($_GET['username']) && $_GET['username'] != ""){
	$username = htmlspecialchars($_GET['username']);
} else {
	$username = "miyagawadai";
}

$url = "https://graph.facebook.com/v4.0/" . $business_id ."?fields=business_discovery.username(" . $username . "){media{timestamp,media_url,like_count,comments_count,caption}}&access_token=" . $access_token;

try {
	$json = json_decode(file_get_contents($url), true);
	$results = $json['business_discovery']['media']['data'];
} catch (Exception $ex){
	$results = "";
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Get Instagram Likes!</title>
	<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.3/css/bulma.min.css">
	<style>
		body {
			margin: 50px 200px 50px;
		}
		.card {
			width: 100%;
		} 
		.card-content{
			height: 120px;
		}
	</style>
</head>
<body>
	<h1 class="title">Get Instagram Likes!</h1>
	<form action="">
		<div class="field">
		  <label class="label">User Name</label>
		  <div class="control">
		    <input class="input" type="text" name="username" placeholder="Instagram username">
		  </div>
		</div>
		<div class="control">
		    <button type="submit" class="button is-link is-light">Show</button>
		 </div>
	</form>
	<hr>
	<?php
		echo "result: " .$username."<br><br><br>";
		echo "<div class='columns'>";
		$i = 1;
		foreach($results as $result){
			echo "<div class='card column'><div class='card-image'><figure class='image is-4by3'><img src=" . $result['media_url'] ."></figure></div><div class='card-content'><div class='content'>" .number_format($result['like_count'])." Like!<br>".mb_substr($result['caption'], 0, 30)."...</div></div></div>" ;
			if($i % 3 == 0) {
				echo "</div><div class='columns'>";
			}
			$i++;
		}
		echo "</div>";
	?>
</body>
</html>

defaultで”miyagawadai”さんのinstaを表示する様にしています。

ほう、
twitterよりも規約が厳しい模様
やってる人もそんなに多くないか…

fastTextで英文のジャンル分類(Text Classification)

### Getting and preparing data
$ wget https://dl.fbaipublicfiles.com/fasttext/data/cooking.stackexchange.tar.gz && tar xvzf cooking.stackexchange.tar.gz
$ ls
cooking.stackexchange.id
$ head cooking.stackexchange.txt
__label__sauce __label__cheese How much does potato starch affect a cheese sauce recipe?
__label__food-safety __label__acidity Dangerous pathogens capable of growing in acidic environments
__label__cast-iron __label__stove How do I cover up the white spots on my cast iron stove?

“__label__” prefix is how fasttext recognize difference of word and label.

$ wc cooking.stackexchange.txt
15404 169582 1401900 cooking.stackexchange.txt
-> split example and validation
$ head -n 12404 cooking.stackexchange.txt > cooking.train
$ tail -n 3000 cooking.stackexchange.txt > cooking.valid

### train_supervised
training.py

import fasttext
model = fasttext.train_supervised(input="cooking.train")

$ python3 training.py
Read 0M words
Number of words: 14543
Number of labels: 735
Floating point exception

何故だ〜〜〜〜〜〜〜〜〜〜

Facebookが取り組む機械学習

Facebookが取り組む機械学習
→ 運営するSNS上のコンテンツを全て理解することを目標
 L 投稿のレコメンデーション
 L 顔や物体の検知
 L 翻訳
 L フェイクニュースの検知

こうやってみると、Facebookは奇抜さはないが、堅実なイメージだ。

facebook SDK with composer

composerを使ったfacebook sdk
https://developers.facebook.com/docs/php/gettingstarted

[vagrant@localhost facebook]$ curl -sS https://getcomposer.org/installer | php
All settings correct for using Composer
Downloading 1.2.2...

Composer successfully installed to: /home/vagrant/facebook/composer.phar
Use it: php composer.phar

続いて、composer.jsonを作成

{
  "require" : {
    "facebook/php-sdk-v4" : "~5.0"
  }
}

その後、php composer.phar installで完了です。

jsonにautoloadを追加

{
  "require" : {
    "facebook/php-sdk-v4" : "~5.0"
  },
  "autoload":{
    "psr-4": {
      "MyApp\\": "lib/"
    }
  }
}

コマンドライン

[vagrant@localhost facebook]$ php composer.phar dump-autoload