blueSky画像複数投稿API。

2024.02.04

Logging

おはようございます、QiitaにblueSky画像複数投稿を行うにはどうしたら良いですか😌と質問が来たので速攻で回答しました。わからないことは聞くしかないよね。大体の人は答えてくれないけど自分はヒントを与えてそれでもわからない場合は答えを渡す。これは昔、自分はその方が良いじゃないかって思っていたけども・・・・どうなんですかねぇ🤔。あまりその人の力にはならないかも。

やはり考える力が必要になる職業なんで、やっぱ闇雲に質問して答えを知っても力にならないじゃないかって思います。悪いことではないけれど技術を手に入れたければ、自分でライブラリのソースコードを読んで頑張らないと駄目なんだろうと思っています。

そうするしか、今も昔も技術力付かないし新人はいろいろな人が書いたソースコードを読むべしだとも思っています、コードを書くよりも読む方が大変です。それ乗り越えないとこの職業は難しい教えてもらうでは身につかないだよなぁ~。

一応、質問の回答のソースコードをこちらでも掲載しておきます。

<?php
require '../vendor/autoload.php';
require 'config.php';
use \potibm\Bluesky\BlueskyApi;
use \potibm\Bluesky\BlueskyPostService;
use \potibm\Bluesky\Feed\Post;

class bluesky
{
    private $api = null;
    private $postService = null;

    public function __construct()
    {
        $this->api = new BlueskyApi(USER_NAME, APP_PASSWORD);
        $this->postService = new BlueskyPostService($this->api);
    }

    /**
     * 簡単なテキスト投稿
     * @param $text
     * @return object
     */
    public function post($text)
    {
        $post = Post::create($text);
        $response = $this->api->createRecord($post);
        return $response;
    }

    /**
     * link付き投稿
     * @param $text
     * @param $url
     * @param $title
     * @param $description
     * @param $optionalimage|null
     * @return object
     */
    public function webPost($text,$url,$title,$description,$optionalimage=null)
    {
        $post = Post::create($text);
        $post = $this->postService->addWebsiteCard(
            $post, 
            $url, 
            $title, 
            $description,
            $optionalimage,
        );
        $response = $this->api->createRecord($post);
        return $response;
    }
    
    /**
     * 画像投稿
     * @param $text
     * @param $ImgLinkAndAltText
     * @return object
     */
    public function imagePost($text, $ImgLinkAndAltText=[])
    {
        $post = Post::create($text);
        $response = null;
        foreach($ImgLinkAndAltText as $val){
            $post = $this->postService->addImage(
                $post,
                $val['img'],
                $val['alt']
            );
        }
        $response = $this->api->createRecord($post);
        return $response;
    }
}
if($argv[0]){
    try {
        var_dump((new bluesky)->post('これはテスト投稿ですよ'));
        //var_dump((new bluesky)->imagePost('これは画像テスト投稿ですよ',[['img'=>'cron.png','alt'=>'クロン'],['img'=>'Gotcha.png','alt'=>'ガチャ']]));
        } catch (\Throwable $th) {
        print $th->getMessage();
        //throw $th;
    }
}

明日へ続く。

タグ

addImage, addWebsiteCard, argv, catch, construct, createRecord, foreach, getMessage, imagePost, PARAM, postService, potibmBlueskyBlueskyApi, potibmBlueskyBlueskyPostService, print, qiita, require, throw, Throwable, use, vendor,

何かの役に立つ#bluesky

2024.01.29

Logging

おはようございます、QiitaにblueSkyのプロフィールURLからRSSを抽出するコードを書きました。先日、blueSkyにRSS機能を追加したという記事を読んだので、その日のうちに対応した形になります。

特に難しいコードでもないので、コメントは一切書いていませんが、それなりに役に立つと信じてリリースしました、ソースの改修などを行って頂けて構いませんが出来ればQiitaもしくはこちらの記事にリンクを貼っていただけたら幸いです。

PHP環境は8.2になっていますが、PHP7系でも動くソースコードなので安心してご使用いただけるかと思います。使用にあたって最終行はコメントアウトを行ってください、url変数も自分にあったurlに変えていただければと思います。

<?php
class blueSkyRss{
    public $rss = null;
    /**
     * __construct
     * @param $url
     * @return void
     */
    public function __construct($url)
    {
        try {
            $html = file_get_contents($url);
            preg_match('/https:\/\/bsky\.app\/profile\/did.*\/rss/',$html,$matches);
            if($rssUrl = $matches[0]){
                $feed = simplexml_load_file($rssUrl);
                $this->rss = $feed;    
            }
        } catch (\Throwable $th) {
            //throw $th;
        }
    }
    /**
     * getRss
     * @return object
     */
    public function getRss():object
    {
        $response = [];
        if(isset($this->rss->channel)){
            $cnt = 0;
            foreach($this->rss->channel->item as $item){
                $response[$cnt]['link']    = $item->link;
                $response[$cnt]['comment']   = $item->description;
                $response[$cnt]['date'] = $item->pubDate;
                $cnt++;
            }
        }
        return (object)$response;
    }
}
$url = 'https://bsky.app/profile/xxxxxxx.bsky.social';
//var_dump((new blueSkyRss($url))->getRss());

明日へ続く。

タグ

bluesky, catch, cnt, construct, description, did, foreach, getRss, isset, lt, object, PARAM, preg_match, pubDate, qiita, return, RSS, throw, Throwable, try,

PHP言語でblueskyの自動投稿を作ってみた。

2024.01.12

Logging

おはようございます。blueskyの自動投稿を作ってみたくなり即興で作りました。因みにblueskyの開発ドキュメントに記載されていたライブラリを使用しています。トライしてみて案外簡単に作れたのでライブラリに感謝だと感じました🙇。なお、自分のブルースカイアカウントのpost/3khojypfnf62zとpost/3khok6quxgj23が自動投稿(コマンドラインからの投稿)になります。

動作環境・sakuraれんたるサーバー・PHP8.2

作成手順

①アプリのパスワードを作成
https://bsky.app/settings/app-passwords
potibm/phluesky(v0.3.0)をインストールする(composerのインストールはご自身で調べてください)

composer require potibm/phluesky:"0.3.0"

③php8.2.bluesky.phpと同じ階層にconfig.phpを作成しdefineを設定する。
画像はphp8.2.bluesky.phpと同じ階層に置いているものとします。
USER_NAMEはプロフィールページに記載されている@の後のあかうんと名.bsky.social、 APP_PASSWORDは①で作成したパスワードになります。

④コマンドラインより実行する。なお、vendorの配置などで参照先は変わります。

php php8.2.bluesky.php
<?php
require '../vendor/autoload.php';
require 'config.php';
use \potibm\Bluesky\BlueskyApi;
use \potibm\Bluesky\BlueskyPostService;
use \potibm\Bluesky\Feed\Post;

class bluesky
{
    private $api = null;
    private $postService = null;

    public function __construct()
    {
        $this->api = new BlueskyApi(USER_NAME, APP_PASSWORD);
        $this->postService = new BlueskyPostService($this->api);
    }

    /**
     * 簡単なテキスト投稿
     * @param $text
     * @return object
     */
    public function post($text)
    {
        $post = Post::create($text);
        $response = $this->api->createRecord($post);
        return $response;
    }

    /**
     * link付き投稿
     * @param $text
     * @param $url
     * @param $title
     * @param $description
     * @param $optionalimage|null
     * @return object
     */
    public function webPost($text,$url,$title,$description,$optionalimage=null)
    {
        $post = Post::create($text);
        $post = $this->postService->addWebsiteCard(
            $post, 
            $url, 
            $title, 
            $description,
            $optionalimage,
        );
        $response = $this->api->createRecord($post);
        return $response;
    }
    
    /**
     * 画像投稿
     * @param $text
     * @param $imgLink
     * @param $altText
     * @return object
     */
    public function imagePost($text, $imgLink, $altText)
    {
        $post = Post::create($text);
        $post = $this->postService->addImage(
            $post,
            $imgLink,
            $altText
        );
        $response = $this->api->createRecord($post);
        return $response;
    }
}
if($argv[0]){
    try {
        var_dump((new bluesky)->post('これはテスト投稿ですよ'));
        var_dump((new bluesky)->imagePost('これは画像テスト投稿ですよ','cron.png','クロン'));    
    } catch (\Throwable $th) {
        print $th->getMessage();
        //throw $th;
    }
}

最後に、cron設定などはご自身で調べてください。
ありがとうございました🙇。

こちらの記事はQiitaで記載されている自分の記事のコピーになります。
https://qiita.com/taoka-toshiaki/items/1508f4e79ea592565cef

明日へ続く。

タグ

addImage, addWebsiteCard, argv, bluesky, catch, Composer, construct, createRecord, define, getMessage, imagePost, PARAM, phluesky, postService, potibm, potibmBlueskyBlueskyApi, potibmBlueskyBlueskyPostService, print, Throwable, vendor,

例外処理をスローする奴(throw=飛ばす?) #php

2023.12.09

Logging

おはようございます、久しぶりにちょっとした技術のお話です。トライキャッチ(try-catch)した後にたまにスローしているコードとか見かけると思います。大体、エラーメッセージとか出力していると思いますが、これ独自のクラスなどに変更することが可能なんですよね。

なので、こんな事が出来るわけです、コケたら違う処理を行ってリカバリみたいな事やコケた値を拾ってバックアップするとか、いろいろな事が出来てしまいます。

PHPの公式でも例文を書いていますが、アレだとなんかわからない方もいると思いますので、もっと簡略化した例文コードを書いてみました。このコードを実行するとゼロによる除算エラーになりますが、トライキャッチ(try-catch)の中に書いているのでエラーにならずcatchへ処理は推移します。そしてスローでオリジナルのクラスを呼び出しているわけです。

<?php
    class errors{
        public function __construct($msg,$x) {
            print $msg;
            print $x;
        }
    }
    try {
        $x=5;
        $a = $x/0;
        //code...
    } catch (\Throwable $th) {
        throw new errors($th->getMessage(),$x);
    }

トライキャッチの良いところはエラーにならないところですが、エラーにならない事が困り物になる場合もあるので注意が必要です。そこは現場の諸先輩方の話を聞いて対応したほうが良さげかと思います。

明日へ続く。

タグ

$msg, $th, $th-&gt, catch, class errors, construct, getMessage, lt, php, print, public function, throw new errors, Throwable, try, try-catch, スロー, リカバリ, 例文, 除算エラー,

#久しぶりのコード`Xにポストするコード`

2023.11.17

Logging

おはよう御座います、さて正月休みは11日ぐらいあります、いやー正月休みはネトフリとゲームざんまいしてみたいと思っているのですが、結局どこかに出ていったりとかして目標達成できずにズルズルと休みを過ごすことになりそうです。👈前と言っていることが違いますね🙇。

さて、久しぶりにコードを書きました、この記事だけ見るとプログラマーじゃないかって思われるので最初に記載しときます。毎日のように仕事ではコードを書いています、仕事以外でコードを書いたのは一週間ぶりかなぁ。でも、このコードはポストでも書いている通り、特に難しいコードでもないのでオープンに公開しました。

このコードには必要ないコードが紛れているのはこのコードが使いましたコードだからです😂。

<?php
date_default_timezone_set('Asia/Tokyo');
ini_set("display_errors", 0);
require_once "../tw-config-v2.php";
require_once "../vendor/autoload.php";

use Abraham\TwitterOAuth\TwitterOAuth;

class merukari{
    public $connection = null;
    public $pdo = null;

    public function __construct()
    {
        $this->connection = new TwitterOAuth(APIKEY, APISECRET,ACCESSTOKEN,ACCESSTOKENSECRET);
        $this->connection->setApiVersion("2");
    }

    public function tweet($pattern)
    {
        $result = $this->connection->post("tweets", ["text" =>$pattern], true);
    }
    
    public static function pattern()
    {
        $pattern = [
            'メルカリで参考書を取り扱ってます📖 どれも綺麗な状態です。#学び #python Cシャープ #プログラム ⏭ https://jp.mercari.com/user/profile/808093563',
            'メルカリで参考書を出品してます📖 どれも綺麗な状態です。#学び #機械学習 #AI #プログラム ⏭ https://jp.mercari.com/user/profile/808093563',
            'メルカリで参考書を売ってます📖 どれも綺麗な状態です。#学び #人工知能 #python #プログラム ⏭ https://jp.mercari.com/user/profile/808093563',
            'メルカリで参考書を取り扱ってますよ📖 どれも綺麗な状態です。#学び #テック #企業 #学び #プログラム ⏭ https://jp.mercari.com/user/profile/808093563',
        ];

        return $pattern[ (int)rand(0,(count($pattern)-1))];
    }
}

try {
    if($argv[1] === 'merukari'){
        (new merukari())->tweet(merukari::pattern());
    }
} catch (\Throwable $th) {
    //throw $th;
}

もしかしたら、以前にもコードを公開して2回目になっていたりするかも知れません。そうだったらごめんなさい、無駄に記事を量産しています(反省。

そうそうXのハッシュタグ廃止は誤解だそうです。思い出したので書いときます、、、。

明日へ続く。

タグ

argv, Asia, catch, connection, construct, int, lt, null, pattern, Python, quot, rand, return, throw, Throwable, true, try, Tweet, use AbrahamTwitterOAuthTwitterOAuth, vendor,