如何 upload/manipulate Apple News JSON 文件?
How to upload/manipulate Apple News JSON files?
我是 Apple News 的独立出版商,使用 News Publisher 创建文章。我一直在寻找我的文章格式和呈现方式的一些灵活性,并且想知道如何为以后的文章上传 JSON 文档 - 我以前从未使用过它。熟悉该服务或语言的人知道如何将这样的东西连接到 Apple News 吗?我已经查看了他们的帮助部分,但问题似乎多于答案。谢谢!
-泰勒
一篇基本的文章是这样的
{
"version": "1.4",
"identifier": "sketchyTech_Demo",
"title": "My First Article",
"language": "en",
"layout": {},
"components": [
{
"role": "title",
"text": "My First Article"
},
{
"role": "body",
"text": "This is just over the minimum amount of JSON required to create a valid article in Apple News Format. If you were to delete the dictionary enclosing this text, you'd be there."
}
],
"componentTextStyles": {}
}
并始终保存为 article.json
。在组件数组中,您可以使用 Apple News Components 中的任何一个。 (注意:您不必使用纯 json,您可以使用 markdown 或 html 来代替文本以简化样式。)
我在 GitHub and you will also find details there about testing your articles with News Preview 上整理了这组更广泛的样本,这将通过列出错误等帮助您。
一旦您准备好上传到您使用的服务,Python 中提供了 API having first registered to do so, examples of implementing the code。您可以单独上传文章,也可以上传包含文章及其链接文件的捆绑包。
已编辑:使用 Python
上传文章
将以下代码复制并粘贴到文本编辑器中并另存为 upload.py
#!/usr/bin/python
import requests
import base64
from hashlib import sha256
import hmac
from datetime import datetime
import glob
import argparse
import os
import mimetypes
from requests.packages.urllib3.filepost import encode_multipart_formdata
from requests.packages.urllib3.fields import RequestField
arg_parser = argparse.ArgumentParser(description='Publish an article using the Apple News API')
arg_parser.add_argument('article_directory', metavar='ARTICLE_DIR', type=str, help='A directory containing an article.json file and resources')
args = arg_parser.parse_args()
channel_id = '[YOUR CHANNEL-ID]'
api_key_id = '[YOUR API-KEY]'
api_key_secret = '[YOUR API KEY-SECRET]'
method = 'POST'
url = 'https://news-api.apple.com/channels/%s/articles' % channel_id
session = requests.Session()
session.verify = False
def part(filename):
name = os.path.basename(filename)
with open(filename) as f:
data = f.read()
part = RequestField(name, data)
part.headers['Content-Disposition'] = 'form-data; filename="%s"; size=%d' % (name, os.stat(filename).st_size)
part.headers['Content-Type'] = 'application/json' if name.endswith('.json') else 'application/octet-stream'
return part
def send_signed_request(method, url, filenames):
body, content_type = encode_multipart_formdata([part(f) for f in filenames])
req = requests.Request(method, url, data=body, headers={'Content-Type': content_type})
req = req.prepare()
date = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
canonical_request = method + url + str(date) + content_type + body
key = base64.b64decode(api_key_secret)
hashed = hmac.new(key, canonical_request, sha256)
signature = hashed.digest().encode('base64').rstrip('\n')
authorization = 'HHMAC; key=%s; signature=%s; date=%s' % (api_key_id, str(signature), date)
req.headers['Authorization'] = authorization
return session.send(req)
response = send_signed_request(method, url, glob.glob('%s/*' % args.article_directory))
print response.status_code
print response.text
接下来将以下元素的值更改为 Apple 提供给您的您自己的值:
channel_id = '[YOUR CHANNEL-ID]'
api_key_id = '[YOUR API-KEY]'
api_key_secret = '[YOUR API KEY-SECRET]'
最后打开终端并从 Finder 中将您创建的 upload.py
文件拖到命令行,然后在拖入包含您的 article.json
文件的文件夹之前留下一个 space 以便两个路径在一行中彼此相邻显示(第一个是 upload.py
文件的位置,第二个是包含 article.json
文件的文件夹的位置)。最后按回车键。
您应该会看到一些已返回 JSON。现在在 iCloud.com 中打开 News Publisher 并导航至 Articles > Drafts from CMS
以获取有关预览和发布已上传文章的说明。
我是 Apple News 的独立出版商,使用 News Publisher 创建文章。我一直在寻找我的文章格式和呈现方式的一些灵活性,并且想知道如何为以后的文章上传 JSON 文档 - 我以前从未使用过它。熟悉该服务或语言的人知道如何将这样的东西连接到 Apple News 吗?我已经查看了他们的帮助部分,但问题似乎多于答案。谢谢!
-泰勒
一篇基本的文章是这样的
{
"version": "1.4",
"identifier": "sketchyTech_Demo",
"title": "My First Article",
"language": "en",
"layout": {},
"components": [
{
"role": "title",
"text": "My First Article"
},
{
"role": "body",
"text": "This is just over the minimum amount of JSON required to create a valid article in Apple News Format. If you were to delete the dictionary enclosing this text, you'd be there."
}
],
"componentTextStyles": {}
}
并始终保存为 article.json
。在组件数组中,您可以使用 Apple News Components 中的任何一个。 (注意:您不必使用纯 json,您可以使用 markdown 或 html 来代替文本以简化样式。)
我在 GitHub and you will also find details there about testing your articles with News Preview 上整理了这组更广泛的样本,这将通过列出错误等帮助您。
一旦您准备好上传到您使用的服务,Python 中提供了 API having first registered to do so, examples of implementing the code。您可以单独上传文章,也可以上传包含文章及其链接文件的捆绑包。
已编辑:使用 Python
上传文章将以下代码复制并粘贴到文本编辑器中并另存为 upload.py
#!/usr/bin/python
import requests
import base64
from hashlib import sha256
import hmac
from datetime import datetime
import glob
import argparse
import os
import mimetypes
from requests.packages.urllib3.filepost import encode_multipart_formdata
from requests.packages.urllib3.fields import RequestField
arg_parser = argparse.ArgumentParser(description='Publish an article using the Apple News API')
arg_parser.add_argument('article_directory', metavar='ARTICLE_DIR', type=str, help='A directory containing an article.json file and resources')
args = arg_parser.parse_args()
channel_id = '[YOUR CHANNEL-ID]'
api_key_id = '[YOUR API-KEY]'
api_key_secret = '[YOUR API KEY-SECRET]'
method = 'POST'
url = 'https://news-api.apple.com/channels/%s/articles' % channel_id
session = requests.Session()
session.verify = False
def part(filename):
name = os.path.basename(filename)
with open(filename) as f:
data = f.read()
part = RequestField(name, data)
part.headers['Content-Disposition'] = 'form-data; filename="%s"; size=%d' % (name, os.stat(filename).st_size)
part.headers['Content-Type'] = 'application/json' if name.endswith('.json') else 'application/octet-stream'
return part
def send_signed_request(method, url, filenames):
body, content_type = encode_multipart_formdata([part(f) for f in filenames])
req = requests.Request(method, url, data=body, headers={'Content-Type': content_type})
req = req.prepare()
date = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
canonical_request = method + url + str(date) + content_type + body
key = base64.b64decode(api_key_secret)
hashed = hmac.new(key, canonical_request, sha256)
signature = hashed.digest().encode('base64').rstrip('\n')
authorization = 'HHMAC; key=%s; signature=%s; date=%s' % (api_key_id, str(signature), date)
req.headers['Authorization'] = authorization
return session.send(req)
response = send_signed_request(method, url, glob.glob('%s/*' % args.article_directory))
print response.status_code
print response.text
接下来将以下元素的值更改为 Apple 提供给您的您自己的值:
channel_id = '[YOUR CHANNEL-ID]'
api_key_id = '[YOUR API-KEY]'
api_key_secret = '[YOUR API KEY-SECRET]'
最后打开终端并从 Finder 中将您创建的 upload.py
文件拖到命令行,然后在拖入包含您的 article.json
文件的文件夹之前留下一个 space 以便两个路径在一行中彼此相邻显示(第一个是 upload.py
文件的位置,第二个是包含 article.json
文件的文件夹的位置)。最后按回车键。
您应该会看到一些已返回 JSON。现在在 iCloud.com 中打开 News Publisher 并导航至 Articles > Drafts from CMS
以获取有关预览和发布已上传文章的说明。