Files
linebot_finance/app/lineinvoicefetcher.py
henry4682 e300632251 feat: Line invoice fetcher testing
1. query line  invoice data
2. TODO line login
2026-03-18 17:45:08 +08:00

71 lines
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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']}")