Skip to content

Commit

Permalink
fix cover
Browse files Browse the repository at this point in the history
  • Loading branch information
malinkang committed Mar 27, 2024
1 parent 943e5d1 commit cf38073
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 30 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/cover.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: fix cover

on:
workflow_dispatch:
jobs:
sync:
name: Sync
runs-on: ubuntu-latest
env:
NOTION_TOKEN: ${{ secrets.NOTION_TOKEN }}
NOTION_PAGE: ${{ secrets.NOTION_PAGE }}
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: fix cover
run: |
python -u scripts/cover.py
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
scripts/__pycache__/
.env
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ requests
notion-client
github-heatmap
retrying
pendulum
pendulum
python-dotenv
Binary file removed scripts/__pycache__/config.cpython-311.pyc
Binary file not shown.
Binary file removed scripts/__pycache__/notion_helper.cpython-311.pyc
Binary file not shown.
Binary file removed scripts/__pycache__/utils.cpython-311.pyc
Binary file not shown.
Binary file removed scripts/__pycache__/weread_api.cpython-311.pyc
Binary file not shown.
73 changes: 73 additions & 0 deletions scripts/cover.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@


import requests
from notion_helper import NotionHelper
import os
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}"
from dotenv import load_dotenv
import utils
load_dotenv()
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()
else:
print(r.text)
return None

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:
results.append(log)

print(f"last date = {last_date}")
if not last_date:
break
return results

def get_run_data(id,name,page_id):
r = requests.get(RUN_LOG_API.format(run_id=id), headers=keep_headers)
shareImg = r.json().get("data").get("shareImg")
cover = utils.upload_cover(shareImg)
notion_helper.client.pages.update(page_id=page_id,cover=utils.get_icon(cover))
if __name__ == "__main__":
notion_helper=NotionHelper()
workouts=notion_helper.query_all(database_id=notion_helper.workout_database_id)
# #查找图片错误的
workouts = [item for item in workouts if "https://images.unsplash.com" in item['cover'].get("external").get("url")]
item_dict = {item.get("properties").get("Id").get("rich_text")[0].get("plain_text"): item for item in workouts}
logs = login()
if logs:

#按照结束时间倒序排序
logs = sorted(logs, key=lambda x: x['endTime'])
for log in logs:

id = log.get("id")
name = log.get("name")
if id not in item_dict:
continue
page_id = item_dict.get(id).get("id")
get_run_data(id,name,page_id)


12 changes: 6 additions & 6 deletions scripts/keep.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import argparse
import json
import os
import time

from dotenv import load_dotenv
import pendulum
from notion_helper import NotionHelper
import requests
Expand All @@ -13,11 +11,12 @@
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",
}

load_dotenv()

def login():
mobile = os.getenv("KEEP_MOBILE")
Expand Down Expand Up @@ -108,8 +107,9 @@ def add_to_notion(workout,end_time,cover):
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}")
cover = utils.upload_cover(cover)
if cover is None:
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"
notion_helper.create_page(
parent=parent, properties=properties,cover=utils.get_icon(cover), icon=icon
)
Expand Down
5 changes: 2 additions & 3 deletions scripts/notion_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

from notion_client import Client
from retrying import retry
from datetime import timedelta

from dotenv import load_dotenv
from utils import (
format_date,
get_date,
Expand All @@ -21,7 +20,7 @@
timestamp_to_date,
get_property_value,
)

load_dotenv()
TAG_ICON_URL = "https://www.notion.so/icons/tag_gray.svg"
USER_ICON_URL = "https://www.notion.so/icons/user-circle-filled_gray.svg"
TARGET_ICON_URL = "https://www.notion.so/icons/target_red.svg"
Expand Down
32 changes: 12 additions & 20 deletions scripts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,28 +338,20 @@ def str_to_timestamp(date):
# 获取时间戳
return int(dt.timestamp())

upload_url = 'https://wereadassets.malinkang.com/'
upload_url = 'https://i.malinkang.com/upload'


def upload_image(folder_path, filename,file_path):
# 将文件内容编码为Base64
def upload_image(file_path):
with open(file_path, 'rb') as file:
content_base64 = base64.b64encode(file.read()).decode('utf-8')

# 构建请求的JSON数据
data = {
'file': content_base64,
'filename': filename,
'folder': folder_path
}

response = requests.post(upload_url, json=data)

if response.status_code == 200:
print('File uploaded successfully.')
return response.text
else:
return None
files = {'file': file}
response = requests.post(upload_url, files=files)
print(response.text)
if response.status_code == 200:
url = 'https://i.malinkang.com'+response.json()[0].get("src")
return url
else:
print("File upload failed")
return None

def url_to_md5(url):
# 创建一个md5哈希对象
Expand Down Expand Up @@ -402,4 +394,4 @@ def download_image(url, save_dir="cover"):

def upload_cover(url):
cover_file = download_image(url)
return upload_image("cover",f"{cover_file.split('/')[-1]}",cover_file)
return upload_image(cover_file)

0 comments on commit cf38073

Please sign in to comment.