Access Alibaba cloud Codeup code base through API, check whether a file is updated and downloaded. By the way, let's introduce Alibaba cloud OpenAPI

demand

There is a NAS at home, which executes scripts regularly every day.
Scripts are written on other computers, and git is used for version management. git is hosted as a private code base created on codeup (Alibaba cloud effect).

Now the requirement is to check whether the script is updated from Codeup before each execution, and download it if any.

There are two solutions:

  1. Install git on NAS, and then synchronize git to the latest before executing the script
  2. Check the last update time of the script on Codeup. If there is an update, download it.

Method 1 because the script is saved with many other things, git can't get a file update from the private library.
So began to study method 2

At the beginning, the idea was to directly use selenium+requests. Selenium realized web page simulation login and extracted cookies, and then requests returned to the web page operation I was familiar with. As a result, it was found that there was sliding verification in the login link, which was not impossible, but at this time, I suddenly found that Codeup had an API!

I sweat (lll ¬ ω ¬) if you have an API, you can go directly to the API.

realization

Let's talk about it here. At present, Alibaba cloud OpenAPI is being updated iteratively between the old and new versions. Some links are invalid or comments are missing. I also looked for a while to find the right place and put together the parameters.

The first is Codeup's API page
https://next.api.aliyun.com/api/codeup/2020-04-14/GetFileBlobs?sdkStyle=old&params={}

Two API s are involved to implement the above functions

  1. Querying the code base submission list: checking for file updates
  2. Query file content: download file content

    Install SDK dependencies first

    then Click to get AK https://usercenter.console.aliyun.com/#/manage/ak , generate accessKeyId and accessSecret parameters

    Then start to find the corresponding API. First, look at the official website documents. The new version of the current document is still missing parameter comments.

    No way. Go back to the Codeup page, directly F12, read several GET and POST records, roughly understand and find out these parameter values. Here I teach you to find the above parameters in one page.

    After filling in the parameters, click initiate call to try to call

    Finally, copy the generated code. Through the two API s mentioned above, you can check for updates and download files.

    Final code:
#!/usr/bin/env python
#coding=utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
import json
import datetime,time
import os

accessKeyId = ""
accessSecret = ""
ProjectId = 
OrganizationId = ""
Ref = ""
FilePath = ""
LocalPath = "Local file location"

# Create AcsClient instance
client = AcsClient(accessKeyId, accessSecret, 'cn-hangzhou')

# Get the file submission update record (the following large section is generated by API)
request = CommonRequest()
request.set_accept_format('json')
request.set_method('GET')
request.set_protocol_type('https') # https | http
request.set_domain('codeup.cn-hangzhou.aliyuncs.com')
request.set_version('2020-04-14')
request.set_content('''{}'''.encode('utf-8'))
request.add_header('Content-Type', 'application/json')
request.add_query_param('OrganizationId', OrganizationId)
request.add_query_param('RefName', Ref)
request.set_uri_pattern(f'/api/v4/projects/{ProjectId}/repository/commits')
request.add_query_param('Path', FilePath)
response = client.do_action_with_exception(request)

# Take out the last update time and compare it with the local file update time
j = json.loads(str(response, encoding = 'utf-8'))
committed_date = j['Result'][0]['CommittedDate']
fileDatetimeCodeup = datetime.datetime.strptime(committed_date,'%Y-%m-%dT%H:%M:%S%z').replace(tzinfo=None)
fileDatetimeLocal = datetime.datetime.fromtimestamp(os.path.getmtime(LocalPath))
if fileDatetimeCodeup > fileDatetimeLocal:

    # You need to download updates from the cloud (the following large sections are generated by the API)
    request = CommonRequest()
    request.set_accept_format('json')
    request.set_method('GET')
    request.set_protocol_type('https') # https | http
    request.set_domain('codeup.cn-hangzhou.aliyuncs.com')
    request.set_version('2020-04-14')
    request.set_content('''{}'''.encode('utf-8'))
    request.add_query_param('OrganizationId', OrganizationId)
    request.add_query_param('FilePath', FilePath)
    request.add_query_param('Ref', Ref)
    request.add_header('Content-Type', 'application/json')
    request.set_uri_pattern(f'/api/v4/projects/{ProjectId}/repository/blobs')
    response = client.do_action_with_exception(request)
    
    # Save file
    j = json.loads(str(response, encoding = 'utf-8'))
    with open(LocalPath,'w',encoding='utf-8'):
        f.write(j['Result']['Content'])

Keywords: Python

Added by dubrubru on Fri, 24 Dec 2021 07:56:25 +0200