Export a single Confluence page as PDF (Python) - python

I have a python script which is trying to export a confluence page as pdf and have tried several methods unsuccessfully:
1.WGET:
wget --ask-password --user xxxxxxxx -O out.pdf -q http://confluence.xxxx.com/spaces/flyingpdf/pdfpageexport.action?pageId=xxxxxxxx
This won't work because it just returns a login dialog rather than the actual pdf.
2.REMOTE API:
Using: https://developer.atlassian.com/confdev/deprecated-apis/confluence-xml-rpc-and-soap-apis/remote-confluence-methods#RemoteConfluenceMethods-Pages
There is an exportSpace method which works but I only want a single page, the getPage method doesn't export to pdf as far as I can tell. Also this is technically deprecated so Atlassian instead recommends:
3.REST API
Using: https://docs.atlassian.com/atlassian-confluence/REST/latest-server/
This doesn't seem to have an option to export a page as PDF
I would appreciate an answer that makes any of these methods work or if you have a completely different approach, I don't care as long as I can get the PDF of the page from a python script.

#This worked for me
#pip install atlassian-python-api
from atlassian import Confluence
#This creates connection object where you provide your confluence URL and credentials.
confluence = Confluence(
url='https://confluence.xxxxx.com/',
username='xxxxxxx',
password='yyyyyyy')
# If you know page_id of the page, you can get page id by going to "Page Information" menu tab and the page id will be visible in browser as viewinfo.action?pageId=244444444444. This will return a response having key:value pairs having page details.
page = confluence.get_page_by_id(page_id=<some_id>)
your_fname = "abc.pdf"
#A function to create pdf from byte-stream responce
def save_file(content):
file_pdf = open(your_fname, 'wb')
file_pdf.write(content)
file_pdf.close()
print("Completed")
#Get your confluence page as byte-stream
response = confluence.get_page_as_pdf(page['id'])
#Call function that will create pdf and save file using byte-stream response you received above.
save_file(content=response)

Based on the answer of Michael Pillai I want to amend that for the Confluence Cloud you must add the api_version='cloud' keyword:
confluence = Confluence(
url='https://confluence.xxxxx.com/',
username='xxxxxxx',
password='yyyyyyy',
api_version='cloud' # <<< important for the pdf export <<<<
)
content = confluence.get_page_as_pdf(page_id)
Copied from the official pdf export example.

You can integrate your script with Confluence Bob Swift CLI plugin. This plugin supports various type of exports.
Step 1: Install the plugin in both places the frontend and backend.
Step 2: Verify your installation by this command -
/location-of-plugin-installation-directory/.confluence.sh --action getServerInfo
Step 3: Use the command below to export your space
/location-of-plugin-installation-directory/.confluence.sh --action exportSpace --space "zconfluencecli" --file "target/output/export/exportSpacepdf.txt" --exportType "PDF"
Link to bob swift plugin

The newest docs use
confluence.export_page(page_id)
For anyone still looking for this info.

Related

Edit confluence page via python script

I need to access a Confluence page via python script and change all 'use_windows' strings to 'use_linux' strings
from sh import sed
from atlassian import Confluence
confluence = Confluence(
url='https://confluence.com/pages/viewpage.action?pageId=89967548'
username='user'
password='passs')
I can use want use this command inside of script:
sed(['-i', 's/use_windows/use_linux/'])
I can see you use the https://github.com/atlassian-api/atlassian-python-api lib. You do NOT need to specify direct URL to page, just Confluence SERVER URL (to core page): "https://confluence.com".
Here is the option:
from atlassian import Confluence
confluence = Confluence(
url='https://my-confluence-site.com',
username='admin',
password='admin')
page_id = 111111 # page id (or you can use space and title)
page_body = get_page_by_id(page_id ).get("body").get("storage").get("value")
page_body = page_body.replace("use_windows", "use_linux")
# set the new body for page
update_existing_page(page_id, title, page_body)

Download a xlsx file by clicking a website button using Python

I'm writing a Python script that creates a COVID-19 dashboard for my country and state and updates it daily.
However, I am struggling to download one of the necessary files.
Basically to download the file I have to access the website (https://covid.saude.gov.br/) and click on a button (class="btn-white md button button-solid button-has-icon-only ion-activatable ion-focusable hydrated ion-activated").
I tried to download via the download link but the site creates a different link every time you click the button and it still has a blob URL before HTTP.
I am very grateful to anyone who tries to help, because the data will be used to monitor the progress of the disease here where I live.
You can use their API to get the file name:
import requests
headers = {
'authority':'xx9p7hp1p7.execute-api.us-east-1.amazonaws.com',
'x-parse-application-id':'unAFkcaNDeXajurGB7LChj8SgQYS2ptm',
}
with requests.Session() as session:
session.headers.update(headers)
resp = session.get('https://xx9p7hp1p7.execute-api.us-east-1.amazonaws.com/prod/PortalGeral').json()
path = resp['results'][0]['arquivo']['url']
The x-parse-application-id doesn't seem to change. If it does, you can get the correct one by querying https://xx9p7hp1p7.execute-api.us-east-1.amazonaws.com/prod/PortalGeralApi and extract it from ['planilha']['arquivo'][url].

Python - IBM Watson Language Translator v3 - uploading content of a file and downloading the result

I'm trying to use the Python SDK for IBM Watson Language Translator v3, testing the beta functionality of translating actual documents. Below is my code:-
from ibm_watson import LanguageTranslatorV3
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
API = "1234567890abcdefg"
GATEWAY = 'https://gateway-lon.watsonplatform.net/language-translator/api'
document_list = []
"""The below authenticates to the IBM Watson service and initiates an instance"""
authenticator = IAMAuthenticator(API)
language_translator = LanguageTranslatorV3(
version='2018-05-01',
authenticator=authenticator
)
language_translator.set_service_url(GATEWAY)
submission = language_translator.translate_document(file="myfile.txt", filename="myfile.txt", file_content_type='text/plain', model_id=None, source='en', target='es', document_id=None)
document_list.append(submission.result['document_id'])
while len(document_list) > 0:
for document in document_list:
document_status = language_translator.get_document_status(document)
if document_status.result['status'] == "available":
translated_document = language_translator.get_translated_document(document)
document_list.remove(document)
language_translator.delete_document(document)
A few questions on this:-
When I check the content of 'translated_document', it doesn't actually contain any content. It contains the headers and the HTTP status of the response but no actually translated content
I decided to use CURL to download my uploaded document and instead of the actual content of the .txt file being uploaded for translation, when downloading the translated file via CURL, it appears that the content is the actual file name (myfile.txt) that is being submitted for translation as opposed to the content of the file.
Researching this and looking at the actual IBM Watson Github respository, it appears that I may have to read the content of 'myfile.txt' to a variable and then pass this variable as 'file={my_variable}' when submitting the translation but doesn't this defeat the object of being able to submit the actual documents for translation? How is this different to the conventional service offered?
Can anybody advise me as to what I'm doing wrong? I've tried multiple approaches (writing the value of 'translated_content' to a file) for example but I just don't seem to be able to grab the translated content nor can I seem to actually upload the content of the file to the service, instead I simply appear to submit the filename.
Thanks all
The file parameter of translate_document is supposed to be the actual content to be translated. I realize that's not clear from the documentation, but that's how the service works. So try passing the actual content you want translated in the file parameter.

Unable to load torrent file using raw base64 form and python xmlrpc client

I am trying to load a torrent file into rtorrent using xmlrpc with the following python3 code:
import xmlrpc.client
server_url = "https://%s:%s#%s/xmlrpc" % ('[REDACTED]', '[REDACTED]', '[REDACTED]');
server = xmlrpc.client.Server(server_url);
with open("test.torrent", "rb") as torrent:
server.load.raw_verbose(xmlrpc.client.Binary(torrent.read()),"d.delete_tied=","d.custom1.set=Test","d.directory.set=/home/[REDACTED]/files")
The load_raw command returns without an error (return code 0), but the torrent does not appear in the rutorrent UI. I seem to be experiencing the same thing as from this reddit post, but I am using the Binary class without any luck.
I am using a Whatbox seedbox.
EDIT:
After enabling logging I am seeing
1572765194 E Could not create download, the input is not a valid torrent.
when trying to load the torrent file, however manually loading the torrent file through the rutorrent UI works fine.
I needed to add "" as the first argument:
server.load.raw_verbose("",xmlrpc.client.Binary(torrent.read()),"d.delete_tied=","d.custom1.set=Test","d.directory.set=/home/[REDACTED]/files")
Not sure why, the docs don't seem to show this is needed.

Openstack Rest API send a file with python to a openstack container

I am trying to upload a video file to Openstack container using REST API.
This is my Python code to upload to the server.
res = requests.put(publicURL+'/'+output_container_name+'/'+toUpload,
headers={'X-Auth-Token': token},
files={'file': open(toUpload,'rb')})
All the variables that you see in code are defined. In fact I can see my file uploaded to the container but when I download it, I cannot play the video.
When I open the video file with a text editor I see these headers at the beginning and at the end of the file.
--0b2e78b52dad4b45a43575c1c42b0b9d
Content-Disposition: form-data; name="file"; filename="input_output.mp4"
.
.
. Normal video content
.
.
--0b2e78b52dad4b45a43575c1c42b0b9d--
How can I get rid of these header in the file?
EDIT: Even when I remove the headers manually there is still some differences in the files when I check them with diff. The Differences are not visible visually the number of lines are the same and everything look the same.
Give the Python OpenStack SDK a try.
pip install openstacksdk
The code for uploading a file.
import sys
from openstack import connection
from openstack import profile
from openstack import utils
utils.enable_logging(True, stream=sys.stdout)
prof = profile.Profile()
prof.set_region(prof.ALL, "RegionOne")
conn = connection.Connection(
auth_url='https://my.identity.endpoint/v2.0/',
profile=prof,
username="my_username",
password="my_password")
c = conn.object_store.create_container(name='videos')
obj = conn.object_store.create_object(container=c.name,
name='input_output.mp4',
data=open('input_output.mp4', 'r'),
content_type='video/mp4')
print(obj)
More info that might be helpful too:
http://python-openstacksdk.readthedocs.org/en/latest/
https://pypi.python.org/pypi/openstacksdk

Categories

Resources