Skip to content

Instantly share code, notes, and snippets.

@monhime
Last active March 8, 2020 01:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monhime/3989531f6577e14a81631ce0b5bb8e99 to your computer and use it in GitHub Desktop.
Save monhime/3989531f6577e14a81631ce0b5bb8e99 to your computer and use it in GitHub Desktop.
コンマ秒の精度でThingSpeak経由で自動ツイート
%% このプログラムについて
% api_keyを取得した自分のapi_keyに書き換えてください
%% 時間設定
% テスト,301,334,448でのPOSTのタイミング
margin = [1.138,1.138,1.138,1.138];
%% 時刻の指定
today_year = year(datetime('now'));
today_month = month(datetime('now'));
today_day = day(datetime('now'));
tomorrow_year = year(datetime('now')+days(1));
tomorrow_month = month(datetime('now')+days(1));
tomorrow_day = day(datetime('now')+days(1));
% テスト(今日22時30分)
dt_st_test = datetime(today_year,today_month,today_day,...
22,10,00,'Format','yyyy MM dd HH:mm:ss');
t_test = datevec(dt_st_test);
% 指定時刻(301)
dt_st_301 = datetime(tomorrow_year,tomorrow_month,tomorrow_day,...
3,01,00,'Format','yyyy MM dd HH:mm:ss');
t_301 = datevec(dt_st_301);
% 指定時刻(334)
dt_st_334 = datetime(tomorrow_year,tomorrow_month,tomorrow_day,...
3,34,00,'Format','yyyy MM dd HH:mm:ss');
t_334 = datevec(dt_st_334);
% 指定時刻(448)
dt_st_448 = datetime(tomorrow_year,tomorrow_month,tomorrow_day,...
4,48,00,'Format','yyyy MM dd HH:mm:ss');
t_448 = datevec(dt_st_448);
dt_st = [dt_st_test,dt_st_301,dt_st_334,dt_st_448];
t = [t_test;t_301;t_334;t_448];
%% 接続設定
tturl='https://api.thingspeak.com/apps/thingtweet/1/statuses/update';
api_key = 'XXXXXXXXXXXXXXXXXXX';
options = weboptions('MediaType','application/x-www-form-urlencoded');
options.Timeout = 10;
% ツイート文字列
tweet_334 = ["test","301","334","448"];
%% 実行
now = datetime('now','Format','yyyy MM dd HH:mm:ss');
for i =1:4
while 1
now = datetime('now','Format','yyyy MM dd HH:mm:ss');
if now < dt_st(i) - minutes(2)
% 投稿2分前まで1分おきに時刻取得
pause(60);
elseif now < dt_st(i) - seconds(20)
% 投稿約20秒前まで1秒おきに時刻取得
pause(1);
else
% 投稿約20秒前に投稿
try
status = 'pre_tweet'+string(2*i-1);
webwrite(tturl, 'api_key', api_key, 'status', status, options);
disp('success (pre)');
catch ME
disp(ME)
FailTwitterPost = true;
end
pause(14)
% 投稿約5秒前に投稿
try
status = 'pre_tweet'+string(2*i);
webwrite(tturl, 'api_key', api_key, 'status', status, options);
disp('success (pre)');
catch ME
disp(ME)
FailTwitterPost = true;
end
% 本投稿
pause(etime(t(i,:),clock)-margin(i));
try
webwrite(tturl, 'api_key', api_key, 'status', tweet_334(i), options);
disp('success (334)');
break; %次の競技へ(iをインクリメント)
catch ME
disp(ME)
FailTwitterPost = true;
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment