pipeline/codes/sample_post.py

57 lines
1.9 KiB
Python
Raw Permalink Normal View History

2023-11-29 15:13:30 +08:00
#! /usr/bin/env python3
import argparse
import json
import os
import requests
user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) " \
"Chrome/74.0.3729.169 Safari/537.36"
header = {"User-Agent": user_agent}
def lims_login():
postUrl = "https://dna.jmdna.com/user/login/auth"
postData = {"userName": "zlseq2", "password": "seq345@JM"}
try:
response = requests.post(postUrl, params=postData, headers=header)
token = json.loads(response.text)["data"]["accessToken"]
except Exception as e:
raise UserWarning('lims连接失败请查看', e)
return token
def lims_search(barcode):
token = lims_login()
payload = {'search[barcode][value]': barcode, "accessToken": token, 'search[report][value]': 0,
'format': '2', 'size': '1000', 'search[barcode][query]:': "eq"}
try:
sample_msg = requests.get("https://dna.jmdna.com/sample/sample/search?", params=payload,
timeout=20, headers=header).text
except Exception as e:
raise UserWarning(f'获取lims系统请求失败 {e}')
lims_infos = json.loads(sample_msg)
code = lims_infos['code']
if code != '-1':
msg = lims_infos['message']
raise UserWarning(f'获取lims系统请求失败 {msg}')
return lims_infos
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="JM get sampleinfo from lims ")
parser.add_argument('-s', '--barcode', help="sample's barcode", required=True)
parser.add_argument('-o', '--output_dir', help="Output directory, default ./", default='')
args = parser.parse_args()
barcorde = args.barcode
sample_data = lims_search(barcorde)
json_out = os.path.join(args.output_dir, 'qc', f'{barcorde}_post.json')
with open(json_out, 'w', encoding='utf8') as ff:
json.dump(sample_data, ff, ensure_ascii=False, indent=4)