Quine Post with Qiita API (Python)

For Quine posts ** here **

I had a lot of trouble escaping when writing quine code as a string using ʻexec`. The code in the article Unicode escapes double quotes and back quotes. If there is a smarter way, please let us know in the comments.

[Version information (sys.version)] 3.8.2 (default, Apr 23 2020, 14:22:33) [GCC 8.3.0]

Code that ran the API in this article

quineRequest.py


authorization_token = 'Bearer 1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdi'
z="\\"
escape_def_code = """
def escape(s):
	return s.replace(\u0022'\u0022, z + 'u0027').replace('\u0022', z * 2 + 'u0022').replace('\u0060', z * 2 + 'u0060')
"""
exec_code = """
import sys
import requests
import json
url = 'https://qiita.com/api/v2/items'
headers = {{
    'Authorization': '{0}',
    'Content-Type': 'application/json'
}}
title = 'Quine post with Qiita API(Python)'
body = '''
For Quine posts**[Here](https://qiita.com/j5c8k6m8/items/bc2324ef036729da1a28)**

\u0060exec\I had a lot of trouble escaping when writing quine code as a string using u0060.
The code in the article Unicode escapes double quotes and back quotes.
If there is a smarter way, please let us know in the comments.

[version information(\u0060sys.version\u0060) ]
**'''
body += sys.version
body += '''**

#Code that ran the API in this article

\u0060\u0060\u0060 python:quineRequest.py
authorization_token = 'Bearer 1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdi'
'''
body += ('z=\u0022' + (z * 2) + '\u0022')
body += '''
escape_def_code = \u0022\u0022\u0022{1}\u0022\u0022\u0022
exec_code = \u0022\u0022\u0022{2}\u0022\u0022\u0022
exec_exec_code = \u0022\u0022\u0022{3}\u0022\u0022\u0022
{3}
\u0060\u0060\u0060
'''

tag = [{{'name': 'Qiita'}}, {{'name': 'Python'}}, {{'name': 'quine'}}, {{'name': 'QiitaAPI'}}, {{'name': 'quine post'}}]
post_data = {{
    'private': False,
    'body': body,
    'title': title,
    'tags': tag,
}}
r = requests.post(url, headers=headers, data=json.dumps(post_data))
"""
exec_exec_code = """
exec(escape_def_code)
exec(exec_code.format(authorization_token, escape(escape_def_code), escape(exec_code), escape(exec_exec_code)))
"""

exec(escape_def_code)
exec(exec_code.format(authorization_token, escape(escape_def_code), escape(exec_code), escape(exec_exec_code)))

Recommended Posts

Quine Post with Qiita API (Python)
Recent ranking creation using Qiita API with Python
Use Trello API with python
POST json with Python3 script
Use Twitter API with Python
Post to slack with Python 3
Web API with Python + Falcon
Play RocketChat with API / Python
Call the API with python3.
Use subsonic API with python3
Qiita API Oauth with Django
[Python] Get user information and article information with Qiita API
Create Awaitable with Python / C API
Get reviews with python googlemap api
Run Rotrics DexArm with python API
[Python] POST wav files with requests [POST]
Hit the Etherpad-lite API with Python
Post multiple Twitter images with python
[python] Read information with Redmine API
Get Qiita trends with Python scraping
Easily post to twitter with Python 3
Collecting information from Twitter with Python (Twitter API)
Try logging in to qiita with Python
Automatically create Python API documentation with Sphinx
Simple Slack API client made with Python
Retrieving food data with Amazon API (Python)
POST variously with Python and receive with Flask
Post Test 3 (Working with PosgreSQL in Python)
Post from another account with Twitter API
[Python] Quickly create an API with Flask
Serverless face recognition API made with Python
[Python] Get Python package information with PyPI API
Implemented Python wrapper for Qiita API v2
FizzBuzz with Python3
Scraping with Python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
I tried to get the authentication code of Qiita API with Python.
python starts with ()
with syntax (Python)
POST photos with Microsoft Bing Image Search API to get Image Insights (Python)
Bingo with python
Get the number of articles accessed and likes with Qiita API + Python
Zundokokiyoshi with python
Qiita, early Python ♪
Excel with Python
Microcomputer with Python
Cast with python
Stock number ranking by Qiita tag with python
Created a Python wrapper for the Qiita API
Text extraction with GCP Cloud Vision API (Python3.6)
Get stock price data with Quandl API [Python]
LINE BOT with Python + AWS Lambda + API Gateway
Post youtube soaring title to twitter with python3
Explosive speed with Python (Bottle)! Web API development
Run VMware vSphere 6 vSphere API with Python script (pyvmomi)