Created
May 10, 2019 16:52
-
-
Save terra-yucco/40cacd476d5febca85e2d403ff28a620 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Usage | |
* | |
* (1) Qiita に 限定共有投稿しておく | |
* (2) 2019-05-30 の GAS のトリガ起動で限定共有投稿は削除され、同内容で公開投稿される | |
* | |
* {限定共有投稿} [2019-05-30]Qiita 記事のタイトル | |
* {公開投稿} Qiita 記事のタイトル | |
*/ | |
// 予約用の記事タイトル日付ヘッダフォーマット | |
var reservationDateFormat = '[yyyy-MM-dd]'; | |
// 投稿時 Tweet しないなら false | |
var tweet = true; | |
/** | |
* トリガ起動で Qiita に記事投稿 | |
*/ | |
function postQiita() { | |
var runDate = Utilities.formatDate(new Date(), 'Asia/Tokyo', reservationDateFormat); | |
var response = callQiitaAPI('/api/v2/authenticated_user/items?page=1&per_page=20', 'GET', ''); | |
var data = JSON.parse(response.getContentText()); | |
for (i = 0; i < data.length; i++) { | |
if (!isTargetArticle(data[i], runDate)) { | |
continue; | |
} | |
postNewArticle(data[i], tweet); | |
deleteOldArticle(data[i].id); | |
} | |
} | |
/** | |
* Qiita 投稿対象記事かの判断 | |
* | |
* @param {object} 記事 | |
* @param {string} スクリプト起動日 | |
* @return {boolean} 投稿処理対象なら true | |
*/ | |
function isTargetArticle(datum, runDate) { | |
if (!datum.private) { | |
return false; | |
} | |
var titleDate = datum.title.slice(0, reservationDateFormat.length); | |
if (titleDate != runDate) { | |
return false; | |
} | |
return true; | |
} | |
/** | |
* 取得した限定共有投稿の内容を利用して新規記事投稿 | |
* タイトルを変更 | |
* | |
* @param {object} 限定共有投稿内容 | |
* @param {boolean} 投稿時に Tweet するか | |
* @return {object} Qiita 投稿 API の response | |
*/ | |
function postNewArticle(draftData, isTweet) { | |
var postData = {}; | |
postData.body = draftData.body; | |
// only for qiita team: coedting | |
// only for qiita team: group_url_name | |
postData.private = false; | |
postData.tags = draftData.tags; | |
postData.title = getNewTitle(draftData.title); | |
postData.tweet = isTweet; | |
return callQiitaAPI('/api/v2/items', 'POST', JSON.stringify(postData)); | |
} | |
/** | |
* 投稿元の限定公開記事を削除 | |
* | |
* @param {string} 削除する Qiita 記事の id | |
* @return {object} 呼び出し API の response | |
*/ | |
function deleteOldArticle(id) { | |
return callQiitaAPI('/api/v2/items/' + id, 'DELETE', ''); | |
} | |
/** | |
* 公開記事用タイトル取得 | |
* | |
* @param {string} 元記事のタイトル | |
* @return {string} 予約投稿用文字列を取り除いたタイトル | |
*/ | |
function getNewTitle(originalTitle) { | |
return originalTitle.slice(reservationDateFormat.length); | |
} | |
/** | |
* Google Apps ScriptからQiita APIを使って、Qiita:Teamの情報を得る方法(ユーザー一覧の入手) | |
* https://qiita.com/takoratta/items/4a0f3a5f9bb75c0646dc | |
* | |
* @param {string} 呼び出す Qiita の API URL | |
* @param {string} 呼び出し時 method | |
* @param {object} API 呼び出し時に指定する payload | |
* @return {object} 呼び出し API の response | |
*/ | |
function callQiitaAPI(endPoint, method, payload) { | |
var scriptProperties = PropertiesService.getScriptProperties(); | |
const QIITA_API_DOMAIN = scriptProperties.getProperty('QIITA_API_DOMAIN'); | |
const QIITA_TOKEN = scriptProperties.getProperty('QIITA_TOKEN'); | |
var headers = { | |
'Authorization': 'Bearer '+ QIITA_TOKEN, | |
'Content-Type' : 'application/json', | |
}; | |
var options = { | |
'method': method, | |
'headers': headers, | |
'payload': payload, | |
}; | |
apiEndPoint = QIITA_API_DOMAIN + endPoint; | |
return UrlFetchApp.fetch(apiEndPoint, options); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment