gpt4 book ai didi

ios - 如何上传/操作 Apple News JSON 文件?

转载 作者:行者123 更新时间:2023-11-28 19:31:37 24 4
gpt4 key购买 nike

我是 Apple News 的独立出版商,使用 News Publisher 创建文章。我一直在寻找我的文章格式和呈现方式的一些灵 active ,并且想知道如何为以后的文章上传 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 上整理了这组更广泛的样本您还可以在那里找到有关使用 News Preview 测试您的文章的详细信息,这将通过列出错误等帮助您。

准备好上传到服务后,您可以使用 API第一次注册这样做,examples of implementing the code在 Python 中提供。您可以单独上传文章,也可以上传包含文章及其链接文件的 bundle 。

已编辑:使用 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]'

最后打开终端并将您创建的 upload.py 文件从 Finder 拖到命令行,然后在拖入包含您的 article.json 的文件夹之前留一个空格文件,以便两个路径在一行中彼此相邻显示(第一个是 upload.py 文件的位置,第二个是包含 article.json 的文件夹的位置 文件)。最后按 Enter。

您应该会看到一些返回的 JSON。现在在 iCloud.com 中打开 News Publisher 并导航到 Articles > Drafts from CMS 以获取有关预览和发布已上传文章的说明。

关于ios - 如何上传/操作 Apple News JSON 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43638767/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com