如何发表电报文章?

时间:2018-02-16 09:36:14

标签: python module

我想使用Python和Telegraph API发布一篇Telegra.ph文章。我试过模块电报和python-telegraphapi,但我不能这样做。我尝试使用模块的示例代码:

from telegraph import Telegraph

telegraph = Telegraph()

telegraph.create_account(short_name='1337')

response = telegraph.create_page(
    'Hey',
    html_content='<p>Hello, world!</p>'
)

print('http://telegra.ph/{}'.format(response['path']))

以及发生了什么:

File "AutoContent.py", line 9, in <module>
    html_content='<p>Hello, world!</p>'
  File "C:\Program Files\Python36\lib\site-packages\telegraph\api.py", line 168, in create_page
    'return_content': return_content
  File "C:\Program Files\Python36\lib\site-packages\telegraph\api.py", line 40, in method
    raise TelegraphException(response.get('error'))
telegraph.exceptions.TelegraphException: PAGE_SAVE_FAILED

另一个代码:

from telegraphapi import Telegraph
telegraph = Telegraph()
telegraph.createAccount("PythonTelegraphAPI")
page = telegraph.createPage("Hello world!", html_content="<b>Welcome, TelegraphAPI!</b>")
print('http://telegra.ph/{}'.format(page['path']))

会发生什么:

File "AutoContent.py", line 6, in <module>
    page = telegraph.createPage("Hello world!", html_content="<b>Welcome, TelegraphAPI!</b>")
  File "C:\Program Files\Python36\lib\site-packages\telegraphapi\main.py", line 139, in createPage
    "return_content": return_content
  File "C:\Program Files\Python36\lib\site-packages\telegraphapi\main.py", line 32, in make_method
    post_request.json()['error'])
telegraphapi.exceptions.TelegraphAPIException: Error while executing createPage: PAGE_SAVE_FAILED

请帮帮我!如何使用Python发布电报文章?

2 个答案:

答案 0 :(得分:0)

我认为您short_name时应该替换create_account

telegraph.create_account(short_name='<your_name>')

答案 1 :(得分:0)

有两种使用该API的解决方案。

import json
MAIN_URL = 'https://api.telegra.ph/'

class apiuz():
    def __init__(self):
        self.http = requests.Session()

    def callMethod(self, n_method=None, a_method=None):
        xitoy2= MAIN_URL + n_method.__name__+'?'
        for x,y in a_method:
            if x!='self' and y!=None: xitoy2+=x+'='+str(y)+'&'
        response = self.http.get(xitoy2[:-1])
        xitoy2 = eval(response.text.replace('\/','/').replace('true','True').replace('false','False'))
        return xitoy2 

        #Methods created by @apiuz
    def createAccount(self, short_name=None, author_name=None, author_url=None):
        return self.callMethod(n_method=self.createAccount, a_method=locals().items())

    def editAccountInfo(self, access_token=None, short_name=None, author_name=None, author_url=None):
        return self.callMethod(n_method=self.editAccountInfo, a_method=locals().items())

    def getAccountInfo(self, access_token=None, field=None):
        return self.callMethod(n_method=self.getAccountInfo, a_method=locals().items())

    def revokeAccessToken(self, access_token=None):
        return self.callMethod(n_method=self.revokeAccessToken, a_method=locals().items())

    def createPage(self, access_token=None, title=None, author_name=None, author_url=None,
        content=None):
        return self.callMethod(n_method=self.createPage, a_method=locals().items())

    def editPage(self, access_token=None, path=None, title=None, content=None,
        author_name=None, author_url=None):
        return self.callMethod(n_method=self.editPage, a_method=locals().items())

    def getPage(self, path=None):
        return self.callMethod(n_method=self.getPage, a_method=locals().items())

    def getPageList(self, access_token=None, offset=0, limit=50):
        return self.callMethod(n_method=self.getPageList, a_method=locals().items())

    def getViews(self, path=None, year=None, month=None, day=None, hour=None):
        return self.callMethod(n_method=self.getPageList, a_method=locals().items())

#or

import requests

params = {
    'access_token': "<ACCES_TOKEN>",
    'path': '/Sample-Test-02-17-4',
    'title': 'My Title',
    'content':[ {"tag":"p","children":["A link to google",{"tag":"a","attrs":{"href":"http://google.com/","target":"_blank"},"children":["http://google.com"]}]} ],
    'author_name': 'My Name',
    'author_url': None,
    'return_content': 'true'
}

url = 'https://api.telegra.ph/editPage'

r = requests.post(url, json=params)
r.raise_for_status()
response = r.json()
print response```