Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
malinkang committed Feb 8, 2024
0 parents commit 1105877
Show file tree
Hide file tree
Showing 11 changed files with 915 additions and 0 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/keep.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: douban book sync

on:
workflow_dispatch:
schedule:
- cron: "0 0 * * *"
jobs:
sync:
name: Sync
runs-on: ubuntu-latest
env:
NOTION_TOKEN: ${{ secrets.NOTION_TOKEN }}
NOTION_PAGE_URL: ${{ secrets.NOTION_PAGE_URL }}
KEEP_MOBILE: ${{ secrets.KEEP_MOBILE }}
KEEP_PASSWORD: ${{ secrets.KEEP_PASSWORD }}
YEAR: ${{ vars.YEAR }}
REPOSITORY: ${{ github.repository }}
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.9
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: douban book sync
run: |
python -u scripts/douban.py "book"
- name: Remove folder
run: rm -rf ./OUT_FOLDER
- name: Set default year if not provided
run: echo "YEAR=$(date +"%Y")" >> $GITHUB_ENV
if: env.YEAR == ''
- name: notion heatmap
run: |
github_heatmap notion --notion_token "${{secrets.NOTION_TOKEN}}" --database_id "${{ env.DATABASE_ID }}" --date_prop_name "日期" --value_prop_name "KM" --unit "KM" --year $YEAR --me "${{secrets.BOOK_NAME}}" --without-type-name --with-animation --background-color=${{ vars.background_color||'#FFFFFF'}} --track-color=${{ vars.track_color||'#ACE7AE'}} --special-color1=${{ vars.special_color||'#69C16E'}} --special-color2=${{ vars.special_color2||'#549F57'}} --dom-color=${{ vars.dom_color||'#EBEDF0'}} --text-color=${{ vars.text_color||'#000000'}}
- name: Rename notion.svg to a random name
run: |
RANDOM_FILENAME=$(uuidgen).svg
mv ./OUT_FOLDER/notion.svg ./OUT_FOLDER/$RANDOM_FILENAME
echo "Renamed file to $RANDOM_FILENAME"
- name: udpate heatmap
run: |
python -u scripts/update_heatmap.py "book"
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
requests
notion-client
github-heatmap
retrying
pendulum
Binary file added scripts/__pycache__/config.cpython-311.pyc
Binary file not shown.
Binary file added scripts/__pycache__/notion_helper.cpython-311.pyc
Binary file not shown.
Binary file added scripts/__pycache__/utils.cpython-311.pyc
Binary file not shown.
Binary file added scripts/__pycache__/weread_api.cpython-311.pyc
Binary file not shown.
25 changes: 25 additions & 0 deletions scripts/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

RICH_TEXT = "rich_text"
URL = "url"
RELATION = "relation"
NUMBER = "number"
DATE = "date"
FILES = "files"
STATUS = "status"
TITLE = "title"
SELECT = "select"
MULTI_SELECT = "multi_select"

workout_properties_type_dict = {
"标题":TITLE,
"距离":NUMBER,
"运动时长":NUMBER,
"平均配速":NUMBER,
"平均心率":NUMBER,
"最大心率":NUMBER,
"消耗热量":NUMBER,
"开始时间":DATE,
"结束时间":DATE,
"Id":RICH_TEXT,
"名字":RICH_TEXT,
}
128 changes: 128 additions & 0 deletions scripts/keep.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import argparse
import json
import os
import time

import pendulum
from notion_helper import NotionHelper
import requests
import utils
from config import workout_properties_type_dict
LOGIN_API = "https://api.gotokeep.com/v1.1/users/login"
RUN_DATA_API = "https://api.gotokeep.com/pd/v3/stats/detail?dateUnit=all&type=running&lastDate={last_date}"
RUN_LOG_API = "https://api.gotokeep.com/pd/v3/runninglog/{run_id}"
keep_headers = {
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0",
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
}


def login():
mobile = os.getenv("KEEP_MOBILE")
password = os.getenv("KEEP_PASSWORD")
data = {"mobile": mobile, "password": password}
r = requests.post(LOGIN_API, headers=keep_headers, data=data)
if r.ok:
print("登录成功")
token = r.json()["data"]["token"]
keep_headers["Authorization"] = f"Bearer {token}"
return get_run_id()


def get_run_id():
last_date = 0
results = []
while 1:
r = requests.get(RUN_DATA_API.format(
last_date=last_date), headers=keep_headers)
if r.ok:
last_date = r.json()["data"]["lastTimestamp"]
records = r.json().get("data").get("records")
logs = [item.get("stats") for sublist in records for item in sublist['logs']]
for log in logs:
if log.get("id")==latest_id:
return results
else:
results.append(log)
print(f"last date = {last_date}")
if not last_date:
break
return results


def get_lastest():
sorts=[
{
"property": "结束时间",
"direction": "descending"
}
]
response = notion_helper.query(database_id=notion_helper.workout_database_id, sorts=sorts,page_size=1)
results = response.get("results")
if len(results)>0:
return utils.get_property_value(response.get("results")[0].get("properties").get("Id"))
else:
return None





def get_run_data(id,name):
r = requests.get(RUN_LOG_API.format(run_id=id), headers=keep_headers)
if r.ok:
workout = {}
data = r.json().get("data")
end_time = pendulum.from_timestamp(data.get("endTime")/1000, tz="Asia/Shanghai")
workout["标题"] = end_time.to_datetime_string()
workout["名字"] = name
workout["Id"] = id
workout["开始时间"] = data.get("startTime")/1000
workout["结束时间"] = data.get("endTime")/1000
workout["距离"] = round(data.get("distance"))
workout["运动时长"] = data.get("duration")
workout["平均配速"] = data.get("averagePace")
workout["消耗热量"] = data.get("calorie")
heartRate= data.get("heartRate")
if heartRate:
workout["平均心率"] = heartRate.get("averageHeartRate")
workout["最大心率"] = heartRate.get("maxHeartRate")
end_time = pendulum.from_timestamp(data.get("endTime")/1000, tz="Asia/Shanghai")
cover= data.get("shareImg")
add_to_notion(workout,end_time,cover)

def add_to_notion(workout,end_time,cover):
properties = utils.get_properties(workout, workout_properties_type_dict)
notion_helper.get_date_relation(properties,end_time)
parent = {
"database_id": notion_helper.workout_database_id,
"type": "database_id",
}
icon = {"type": "emoji", "emoji": "🏃🏻"}
#封面长图有限制
if cover and len(cover) <=2000:
pass
else:
cover="https://images.unsplash.com/photo-1547483238-f400e65ccd56?q=80&w=2970&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
print(f"cover = {cover}")
notion_helper.create_page(
parent=parent, properties=properties,cover=utils.get_icon(cover), icon=icon
)



if __name__ == "__main__":
parser = argparse.ArgumentParser()
notion_helper=NotionHelper()
latest_id = get_lastest()
logs = login()
#按照结束时间倒序排序
logs = sorted(logs, key=lambda x: x['endTime'])
for log in logs:
id = log.get("id")
if id == latest_id:
break
name = log.get("name")
get_run_data(id,name)
Loading

0 comments on commit 1105877

Please sign in to comment.