This article is participating in Python Theme Month. See the link to the event for more details
background
The June challenge is over. Although the time of a month is very long, but also passed quickly, the process of writing is also happy. But from day one, when you clocked in, a seed of automatic Posting was quietly planted. During the read a lot of automatic release of the article, although the function can be completed, but there are always some imperfect place. With the implementation of the login feature and the implementation of automatic login, the automatic publishing feature is a breeze.
Reading this article is recommended as a priorityThe realization of automatic login of nugget
Technical background
A brief introduction to the technology used in this article
- Linux scheduled task Linux crontab command
- Python Network Requests: Let HTTP serve humans
- Simple use of Github automated deployment of Github actions
Automatic release
The content of this article needs to be established after the successful login of nuggets, if you want to know the login of nuggets can see my other two articles.
- Nugget login triggered thinking
- The realization of automatic login of nugget
1. Encapsulate the request object
Here, after the successful login cookie is encapsulated into the request.session object, each subsequent request will be automatically carried.
1.1 the cookie encapsulation
import requests
from requests import cookies
driver_cookies = [{'domain': '.juejin.cn'.'expiry': 1632996319.'httpOnly': False.'name': 'MONITOR_WEB_ID'.'path': '/'.'secure': False.'value': 'f9c24adb-a882-4189-a44a-1546a55edbd2'}]
session = requests.session()
if driver_cookies is False:
raise Exception("Cookie is Blank")
for cookie in driver_cookies:
cookie_obj = requests.cookies.create_cookie(
domain=cookie.get("domain"),
name=cookie.get("name"),
value=cookie.get("value")
)
session.cookies.set_cookie(cookie_obj)
Copy the code
1.2 Request Encapsulation
The premise of encapsulation here is that the mined interface returns a uniform format:
- A unified
HTTP STATUS CODE
, the default value is 200 - Uniform data format The format of data returned is uniform
json
(Corresponds to the python dictionary type)
# return only requests with status_code 200 and the data format is dictionary
def request(self, *args, **kwargs) :
response = session.request(*args, **kwargs)
ifresponse.status_code ! =200:
raise Exception("Request error")
return response.json()
Copy the code
2. Test requests
This interface test uses the list of the draft box, and returns the list of the corresponding articles in the draft box.
result = request('post'."https://api.juejin.cn/content_api/v1/article_draft/query_list")
print(result)
[{'id': '6980289137664327688'.'article_id': '0'.'user_id': '993614678985085'.'category_id': '0'.'tag_ids': [].'link_url': ' '.'cover_image': ' '.'is_gfw': 0.'title': 'articles'.'brief_content': ' '.'is_english': 0.'is_original': 1.'edit_type': 10.'html_content': ' '.'mark_content': ' '.'ctime': '1625225445'.'mtime': '1625225447'.'status': 0.'original_type': 0}]
Copy the code
3. Publish
Finally, it’s the exciting time. We can finally publish articles using the program. Direct code
Note note Note that the post must be ready to be sent, the required field classification, tag, and summary must meet the requirements to be published.
# draft_id corresponds to the id of the article in the draft box
json = {
"draft_id": "6980289137664327688"."sync_to_org": False."column_ids": []
}
result = request('post'."https://api.juejin.cn/content_api/v1/article/publish", json=json)
print(result)
If # err_no is 0, the message is sent successfully
{'err_no': 0.'err_msg': 'success'.'data': {
'article_id': '6980297603929866271'.#...
'org_id': '0'}}Copy the code
4. The message is sent successfully
Open my homepage of nuggets and you can find that an article has been sent successfully through our program 😭😭😭
Project deployment
Let’s start with a family photo of this code
├ ─ ─ LICENSE ├ ─ ─ Pipfile ├ ─ ─ Pipfile. Lock ├ ─ ─ the README. Md ├ ─ ─ the core │ ├ ─ ─ just set py │ ├ ─ ─ config. Py │ ├ ─ ─ juejin. Py │ └ ─ ─ Track.py ├── driver │ ├─ Linux │ ├─ ├─ MAC │ ├─ chromedriver │ ├─ main.py ├─ requeirments.txt ├─ tempCopy the code
1. Create a Github repository
I’m not going to go into the details of how to create a repository and upload the code, but just look at the results, okay
2. Create a Github Action task
Write directly to the python run configuration file. See Simple Use of Github Action for details
Note: Cron is used here cron: 00 02 * * *
The script is executed at two o ‘clock every day.
name: Python package
on:
push:
branches:
- master
schedule:
- cron: 00 02 * * *
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: | python -m pip install --upgrade pip pip install -r requeirments.txt - name: execute py script
env:
JUEJIN_USERNAME: The ${{ secrets.JUEJIN_USERNAME }}
JUEJIN_PASSWORD: The ${{ secrets.JUEJIN_PASSWORD }}
JUEJIN_NICKNAME: The ${{ secrets.JUEJIN_NICKNAME }}
run: | python main.pyCopy the code
3. Add environment variables
Add environment variables to the GitHub Setting
variable | describe | The sample | use |
---|---|---|---|
JUEJIN_USERNAME | Nuggets login account | xxx | The login |
JUEJIN_PASSWORD | Nugget login password | xxx | The login |
JUEJIN_NICKNAME | The nuggets nickname | Fried rice with tomato and egg | Check whether the login is successful |
4. View the result
The results can be viewed in the Github Action
My execution results are:
Write in the last
The article you are reading is automatically published, thank you for coming.
The project is in continuous iteration and update. Your valuable opinions are welcome.
If you think my project is helpful to you, please click ❤️❤️❤️.
See all the code for this projectMy GitHub repositoryWelcome to Star Fork.