diff --git a/app/lineinvoicefetcher.py b/app/lineinvoicefetcher.py new file mode 100644 index 0000000..6f6ebe0 --- /dev/null +++ b/app/lineinvoicefetcher.py @@ -0,0 +1,71 @@ +import requests + +SESSION_TOKEN = "r:df33fc57ed1b792131eb0eb24f8715a3" + +headers = { + "Cookie": "_ldbrbid=tr__k1y/XGTcLDWeHW1fMcteAg==", + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)", + "x-parse-session-token": SESSION_TOKEN, + "x-parse-application-id": "line-invoice", + "Content-Type": "application/json", +} + +def get_invoices(period_code, end_cursor=None): + payload = { + "operationName": "GetInvoices", + "variables": { + "periodCode": period_code, + **({"endCursor": end_cursor} if end_cursor else {}) + }, + "query": """query GetInvoices($periodCode: String!, $endCursor: String) { + invoices( + where: {periodCode: {equalTo: $periodCode}, status: {notEqualTo: "ERROR"}} + first: 50 + after: $endCursor + ) { + edges { + node { + amount + brandName + category + createdAt + invoiceDate + lineItems { + ... on Element { + value + } + } + sellerName + serial + } + } + pageInfo { + hasNextPage + endCursor + } + } +}""" + } + r = requests.post("https://invoice.line.me/graphql", headers=headers, json=payload) + return r.json() + +# 拿所有發票(自動翻頁) +def get_all_invoices(period_code): + all_invoices = [] + end_cursor = None + while True: + data = get_invoices(period_code, end_cursor) + edges = data["data"]["invoices"]["edges"] + all_invoices.extend([e["node"] for e in edges]) + page_info = data["data"]["invoices"]["pageInfo"] + if not page_info["hasNextPage"]: + break + end_cursor = page_info["endCursor"] + return all_invoices + +# 民國期別:11401=1-2月, 11403=3-4月, ..., 11412=11-12月 +invoices = get_all_invoices("11502") +print(f"共 {len(invoices)} 張發票") +for inv in invoices: + # print(f"{inv}") + print(f"{inv['invoiceDate'][:10]} {inv['brandName']} {inv['sellerName']} ${inv['amount']}") \ No newline at end of file