I decided to use wordpress to create a web design gallery site specializing in treatment clinics in connection with my side business. However, it is very troublesome to save the capture for the gallery site image and update to wordpress every time. So I created an automatic posting environment after studying python. Since it will be long, I will post it in several times.
\ * Auto-posting here refers to running a python script on the terminal, taking screen captures, creating post text, logging in to worpdress, and posting.
The actual gallery site will be here.
image_cap
├── cap.py #Capture save, csv write script
├── images #Where to save the captured image
├── min-images #Where to save the compressed captured image
├── list.txt #Gallery-Get the URL of the site you want to add to
├── up-list.csv #CSV used for bulk posting
├── upload.py #Script for wordpress posting
├── setting.config #Settings such as login ID, password and image directory path
For the time being, the source code is managed on github. Please click here.
Software for programmatically running a web browser used for capturing captures. It seems to be used mainly for testing websites.
$ pip install selenium
I want to use Chrome this time, so download it from homebrew.
$ brew install chromedriver
$ which chromedriver
usr/local/bin/chromedriver #Set this as a selenium pass
It is used to get the title of the posted site by scraping.
$ pip install beautifulsoup4
To use the tinypng API for image compression. The post of here was very helpful. Please also set the API KEY.
$ pip install tinify
Installed to use lxml as an html parser. To be honest, I don't know what the parser will do. .. Sweat
pip install lxml
I referred to here.
Variables such as login ID and file path are summarized using python's ConfigParser to make it easier for future fixes. We will use this in the code below.
setting.config
[CAPTURE] #Captured image path
img_pass = /cap_git/images
min_img_pass = /cap_git/min_image
[RESIZE] #tinypng API key
tiny_key = hogehoge
[WRITE] #csv write settings
init_row = post_title,post_category,capture,url,post_type,post_status
post_type = post
post_status = publish
up_address = http://test/wp-content/themes/test/dist/images/
[UPLOAD] #Wordpress login information etc.
login_url = http://test/wp-login.php
login_id = hoge
login_pass = hogehoge
import_url = http://test/wp-admin/admin.php?import=csv
img_up_pass = http://test/wp-content/themes/sage-8.5/dist/images
So in cap.py load the variables below
cap.py
#Read configuration file
conf = configparser.ConfigParser()
conf.read('setting.config')
#API key settings
tinify.key = conf['RESIZE']['tiny_key']
#Dir with unreduced image
FROM_DIR = conf['CAPTURE']['img_pass']
#Place the reduced image dir
MIN_DIR = conf['CAPTURE']['min_img_pass']
#Setting items
INIT_ROW = list(conf['WRITE']['init_row'].split(','))
#Image URL
IM_ADRRESS = conf['WRITE']['up_address']
#Post type to set
POST_TYPE = conf['WRITE']['post_type']
POST_STATUS = conf['WRITE']['post_status']
Module configuration with configparser.ConfigParser () Specify the path of the confg file in conf.reade and read it. Since the value can be accessed in a dictionary-like format, I insert it into a variable.
For bulk posting in csv. I referred to here. In addition, Advanced Custom Fields also sets custom fields for posting.
Continue to save part 2 screen capture
Recommended Posts