Wordpress.com: Media, List
Wordpress.com: Media, List
Lists the URLs of the content in the media library. Up to 100 items are listed in order with the newest first. It is possible to filter by the uploaded datetime. For example, it is used for monitoring public files that can be viewed from the Internet.
Configs
  • U: Select HTTP_Authz Setting *
  • A: Set WordPress.Com Domain (e.g. “example.com” or ID) *#{EL}
  • B1: Set Start Datetime of Duration (eg “2021-12-31 09:00”) *#{EL}
  • B2: Set End Datetime of Duration (eg “2022-01-01 09:00”) *#{EL}
  • C1: Select STRING that stores URL (update) *
  • C2: Select STRING that stores ID-Datetime-Mime-URL TSV (update)
Script (click to open)
// GraalJS Script (engine type: 2)

//////// START "main()" /////////////////////////////////////////////////////////////////

main();
function main(){ 

//// == Config Retrieving / 工程コンフィグの参照 ==
const strAuthzSetting     = configs.get      ( "AuthzConfU" );   /// REQUIRED
  engine.log( " AutomatedTask Config: Authz Setting: " + strAuthzSetting );
const strWpcomDomain      = configs.get      ( "StrConfA" );     /// REQUIRED
  if( strWpcomDomain    === "" ){
    throw new Error( "\n AutomatedTask ConfigError:" +
                     " Config {A: WordPress.com domain} is empty \n" );
  }
const strDurationStart    = configs.get      ( "StrConfB1" );    // NotRequired
const strDurationEnd      = configs.get      ( "StrConfB2" );    // NotRequired
const strPocketUrls       = configs.getObject( "SelectConfC1" ); /// REQUIRED
const strPocketTsv        = configs.getObject( "SelectConfC2" ); // NotRequired


//// == Data Retrieving / ワークフローデータの参照 ==
// (Nothing. Some workflow data is referenced via Expression Language in Config.)


//// == Calculating / 演算 ==

/// Get a list of items in the media library.
/// WordPress.com REST API
/// https://developer.wordpress.com/docs/api/
/// https://developer.wordpress.com/docs/api/1.1/get/sites/%24site/media/
// request1, prepare
let request1Uri = "https://public-api.wordpress.com/rest/v1.1/sites/" +
                   strWpcomDomain + "/media/";
let request1    = httpClient.begin(); // HttpRequestWrapper
    request1    = request1.authSetting( strAuthzSetting ); // with "Authorization: Bearer XX"
    // https://questetra.zendesk.com/hc/en-us/articles/360024574471-R2300#HttpRequestWrapper
    request1    = request1.queryParam( "number", "100" );
    if( strDurationStart !== "" ){
      let dateDurationStart = toJsDate( strDurationStart );
      request1  = request1.queryParam( "after", dateDurationStart.toISOString() );
    }
    if( strDurationEnd   !== "" ){
      let dateDurationEnd   = toJsDate( strDurationEnd );
      request1  = request1.queryParam( "before", dateDurationEnd.toISOString() );
    }
// request1, try
const response1     = request1.get( request1Uri ); // HttpResponseWrapper
engine.log( " AutomatedTask ApiRequest1 Start: " + request1Uri );
const response1Code = response1.getStatusCode() + "";
const response1Body = response1.getResponseAsString() + "";
engine.log( " AutomatedTask ApiResponse Status: " + response1Code );
if( response1Code !== "200"){
  throw new Error( "\n AutomatedTask UnexpectedResponseError: " +
                    response1Code + "\n" + response1Body + "\n" );
}
// response1, parse
/* 
engine.log( response1Body ); // debug
{
  "found": 121,
  "media":[{
    "ID":3784,
    "URL":"https:\/\/example.blog\/wp-content\/uploads\/2021\/06\/example.png",
    "guid":"https:\/\/example.blog\/wp-content\/uploads\/2021\/06\/example.png",
    "date":"2021-06-10T13:57:59+09:00",
    "post_ID":0,
    "author_ID":1,
    "file":"example.png",
    "mime_type":"image\/png",
    "extension":"png",
    "title":"example",
    "caption":"",
    "description":"",
    "alt":"",
    "icon":"https:\/\/example.blog\/wp-includes\/images\/media\/default.png",
    "size":"40.62 KB",
    "thumbnails":{ # # # },
    "height":675,
    "width":1200,
    "exif":{ # # # }
  }],
  "meta":{
     next_page:"value=2019-02-14T23%3A37%3A57%2B09%3A00&id=2809"
  }
}
*/
const response1Obj = JSON.parse( response1Body );
engine.log( " AutomatedTask #Media: " + response1Obj.media.length );

let strUrls = "";
for( let i = 0; i < response1Obj.media.length; i++ ){
  strUrls += response1Obj.media[i].URL;
  if( i !== response1Obj.media.length - 1 ){
    strUrls += "\n";
  }
}
let strTsv = "";
for( let i = 0; i < response1Obj.media.length; i++ ){
  strTsv += response1Obj.media[i].ID;
  strTsv += "\t";
  strTsv += response1Obj.media[i].date;
  strTsv += "\t";
  strTsv += response1Obj.media[i].mime_type;
  strTsv += "\t";
  strTsv += response1Obj.media[i].URL;
  if( i !== response1Obj.media.length - 1 ){
    strTsv += "\n";
  }
}


//// == Data Updating / ワークフローデータへの代入 ==
engine.setData( strPocketUrls, strUrls );
if( strPocketTsv    !== null ){
  engine.setData( strPocketTsv, strTsv );
}

} //////// END "main()" /////////////////////////////////////////////////////////////////


function toJsDate( bpmsDateOrDatetimeStr ){
  // BPMS Date:     "2020-04-01"  (subtype "Y/M" "M/D" "Y", not supported)
  // BPMS Datetime: "2020-04-01 23:59"
  let year       = 0;
  let monthIndex = 0;
  let day        = 0;
  let hours      = 0;
  let minutes    = 0;

  //  The ECMA/JavaScript Date object has a large number of methods.
  // "Date.parse" is danger (strongly discouraged)
  // - new Date("2014-11-10") // Mon Nov 10 2014 09:00:00 GMT+0900 (JST)
  // - new Date(2014, 10, 10) // Mon Nov 10 2014 00:00:00 GMT+0900 (JST)
  let arrDatetime = bpmsDateOrDatetimeStr.split(" ");
  if( arrDatetime.length === 1 ){
    let arrDateParts = arrDatetime[0].split("-");
    year       = parseInt(arrDateParts[0], 10);
    monthIndex = parseInt(arrDateParts[1], 10) - 1;
    day        = parseInt(arrDateParts[2], 10);
  }
  if( arrDatetime.length === 2 ){
    let arrDateParts = arrDatetime[0].split("-");
    let arrTimeParts = arrDatetime[1].split(":");
    year       = parseInt(arrDateParts[0], 10);
    monthIndex = parseInt(arrDateParts[1], 10) - 1;
    day        = parseInt(arrDateParts[2], 10);
    hours      = parseInt(arrTimeParts[0], 10);
    minutes    = parseInt(arrTimeParts[1], 10);
  }
  return new Date( year, monthIndex, day, hours, minutes );
}


/*
Notes-en:
- Lists the files stored in "Media" in your WordPress.com account.
    - WordPress.com is a service that hosts WordPress sites. It is provided by Automattic itself.
    - Monitor files that are available for viewing from the Internet.
- By specifying the time range, you can narrow down by the uploaded time.
    - You can specify date data instead of date and time data. (Considered as 00:00)
- For example, "all images uploaded the day before" can be automatically extracted every morning.
    - By placing "HTML mail generation/send" in the downstream, you can check images by HTML mail.
    - Email-HTML String, Create ImageList
    - https://support.questetra.com/ja/addons/email-html-string-create-imagelist-2021/
    - By placing "Append Spreadsheet" in the downstream, you can automate the management of content.
    - Google Sheets: Sheet, Append TSV
    - https://support.questetra.com/addons/google-sheets-sheet-append-tsv/
    - Information leakage risk, Typographical errors, Brand guidelines, etc.

APPENDIX-en
- You can also refer to it by "Site ID" (blogid) instead of the WordPress.Com domain.
    - Site ID (blogid) is obtained from `GET v1.1/sites/example.blog/` and the "View page source".
- Request is made to the "WordPress.com REST API" (WP.COM API).
    - https://developer.wordpress.com/docs/api/
    - Note: Not the "WP.REST API" for WordPress.Org.
- Setting example of "HTTP Authentication" (OAuth2)
    - Authorization Endpoint URL:
        - https://public-api.wordpress.com/oauth2/authorize
    - Token Endpoint URL:
        - https://public-api.wordpress.com/oauth2/token
    - Scope:
        - "global", "", "auth" or "media"
        - (document) https://developer.wordpress.com/docs/oauth2/
        - By default, the token will grant the application full access to a single blog.
        - Needs a "global" scope, if to access to all the blogs that users have on WordPress.com.
    - Client ID, Consumer Secret:
        - ( from https://developer.wordpress.com/apps/ )
        - Redirect URLs: https://s.questetra.net/oauth2callback

Notes-ja:
- WordPress.com アカウントの「メディア」に格納されているファイルをリストアップします。
    - WordPress.com は WordPress sites をホストするサービスです。Automattic 社自身が提供しています。
    - インターネットから閲覧できる状態にある新着ファイルを監視します。
- 時刻範囲を指定することで、アップロード時刻による絞り込みが可能です。
    - 日時データではなく、日付データでも指定可能です。(その場合、00:00とみなされます)
- たとえば「前日にアップロードされた全ての画像」を毎朝自動抽出することができます。
    - 下流工程に『HTMLメール生成/送信』を配置すれば "全ての画像" をHTMLメールにて総覧できます。
    - Email-HTML 文字列, 画像リスト 生成
    - https://support.questetra.com/ja/addons/email-html-string-create-imagelist-2021/
    - 下流工程に『スプレッドシート追記』を配置すれば、コンテンツ更新の管理を自動化できます。
    - Google スプレッドシート: Sheet, TSVデータを追記
    - https://support.questetra.com/ja/addons/google-sheets-sheet-append-tsv/
    - 情報漏洩リスクの低減、誤字脱字の検知、ブランドガイドラインの運用、など

APPENDIX-ja:
- WordPress.Com ドメインの代わりに「サイトID」(blogid)で参照することも可能です。
    - サイトID(blogid)は、"`GET v1.1/sites/example.blog/`" や "ページのソース" 等から取得します。
- "WordPress.com REST API" (WP.COM API) に対してリクエストが投げられます。
    - https://developer.wordpress.com/docs/api/
    - 注: WordPress.Org 用の "WP.REST API" ではありません。
- "HTTP認証設定" の例 (OAuth2)
    - 認可エンドポイント URL:
        - https://public-api.wordpress.com/oauth2/authorize
    - トークンエンドポイント URL:
        - https://public-api.wordpress.com/oauth2/token
    - スコープ:
        - "global", "", "auth" or "media"
        - (document) https://developer.wordpress.com/docs/oauth2/
        - デフォルトでは、1つの blog に対するアクセスが認可されます。
        - 全保有 blogs へアクセスできるようにするには "global" をセットします。
    - クライアントID, クライアントシークレット:
        - ( 開発者ページから取得してください⇒ https://developer.wordpress.com/apps/ )
        - Redirect URLs: https://s.questetra.net/oauth2callback
*/

Download

2021-06-24 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/wordpress-com-media-list-2021/
The Add-on import feature is available with Professional edition.

Notes

  • Lists the files stored in “Media” in your WordPress.com account.
    • WordPress.com is a service that hosts WordPress sites. It is provided by Automattic itself.
    • Monitors for new files that are available to be viewed from the Internet.
  • By specifying the time range, you can filter by uploaded time.
    • You can specify date data instead of date and time data (considered as 00:00).
  • For example, all images uploaded the day before can be automatically extracted every morning.

Capture

Lists the URLs of the content in the media library. Up to 100 items are listed in order of new arrival. It is possible to narrow down by the uploaded datetime. For example, it is used for monitoring public files that can be viewed from the Internet.
Check images using HTML email
Check images using HTML email (Gmail)
Lists the URLs of the content in the media library. Up to 100 items are listed in order of new arrival. It is possible to narrow down by the uploaded datetime. For example, it is used for monitoring public files that can be viewed from the Internet.

Appendix

See also

1 thought on “WordPress.com #Media: List”

  1. Pingback: Wordpress.com: Media, Upload – Questetra Support

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d