Created
January 8, 2022 20:02
-
-
Save asukaminato0721/afd560577b81c6150aafec3491f78baa to your computer and use it in GitHub Desktop.
WIP. a wrapper about integrate.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
usage: python3 this_file.py expr | |
for example: | |
python3 this_file.py "tan(x)" | |
python3 this_file.py "^(x,-1)" | |
python3 this_file.py "+(x,-1)" | |
python3 this_file.py "+(-1,x,^(x,-1))" | |
""" | |
import json | |
import sys | |
import uuid | |
from typing import Dict, List, Union | |
import requests | |
from lxml.etree import HTML | |
def query_int(expr: str): | |
""" | |
get result of int | |
""" | |
cookies = { | |
"consentUUID": uuid.uuid4().hex, | |
"gdpr": "0", | |
} | |
headers = { | |
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4658.57 Safari/537.36", | |
"Origin": "https://www.integral-calculator.com", | |
"Referer": "https://www.integral-calculator.com/", | |
} | |
data_q: Dict[str, Union[str, int, bool]] = json.loads( | |
'{"expressionCanonical":"x","intVar":"x","complexMode":false,"keepDecimals":false,"alternatives":{},"f":"F","maximaFoundAnElementaryAntiderivative":true}' | |
) | |
data_q["expressionCanonical"] = expr | |
data = { | |
"q": json.dumps(data_q), | |
"v": "1630214470", | |
} | |
response = requests.post( | |
"https://www.integral-calculator.com/manualint.php", | |
headers=headers, | |
cookies=cookies, | |
data=data, | |
) | |
return response.text | |
def extract_results(results: str) -> List[str]: | |
""" | |
extract result from html | |
""" | |
html = HTML(results) # type: ignore | |
return html.xpath("//div[starts-with(@class, 'calc-math')]/text()") | |
def main(expr: str): | |
"""main function""" | |
tex = extract_results(query_int(expr)) | |
print(*tex, sep="\n") | |
main(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment