Skip to content

Commit a7c104a

Browse files
author
徳住友稜
committed
fix total size
1 parent edf0ad3 commit a7c104a

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

qiita_notification/send_new_iine_dev.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import json
22
import os
3-
from math import ceil
4-
from typing import List, Dict, Any, Union, Tuple
5-
import json
3+
from typing import List, Dict, Any
64
from urllib.request import Request
7-
from urllib import request, parse, error
5+
from urllib import request, parse
86
from http.client import HTTPResponse
97

108

@@ -70,29 +68,33 @@ def serialize_record(record: Dict[str, Any]) -> Dict[str, Any]:
7068

7169
new = record.get('dynamodb', {}).get('NewImage')
7270
title = new.get('title', {}).get('S', '')
71+
new_iine = int(new.get('iine', {}).get('N', 0))
72+
7373
return {
7474
'ids': ids,
7575
'title': title,
76+
'new_iine': new_iine,
7677
'past_iine': past_iine
7778
}
7879

7980

80-
def serialize_response_name(response: Response, num: int, title: str) -> Dict[str, Any]:
81+
def serialize_response_name(response: Response, new_size: int, num: int, title: str) -> Dict[str, Any]:
8182
"""serialize iine data of Qiita API v2
8283
:param response:
8384
:return:
8485
"""
85-
size = len(response.body) - num
86+
size = new_size - num
8687
if size <= 0:
8788
users: List[str] = []
88-
89-
new_iine = response.body[:size]
90-
users = [
91-
resp.get('user', {}).get('id') for resp in new_iine
92-
]
89+
else:
90+
new_iine = response.body[:size]
91+
users = [
92+
resp.get('user', {}).get('id') for resp in new_iine
93+
]
9394
return {
9495
'title': title,
95-
'users': users
96+
'users': users,
97+
'size': size
9698
}
9799

98100

@@ -104,11 +106,13 @@ def get_new_iine(item: Dict[str, Any], token: str) -> Dict[str, Any]:
104106
headers = {'Authorization': 'Bearer {}'.format(token)}
105107
ids = item.get('ids', '')
106108
past_iine = item.get('past_iine', 0)
109+
new_iine = item.get('new_iine', 0)
107110
url = f'https://qiita.com/api/v2/items/{ids}/likes'
108111

109112
response = req_get(url, headers=headers)
110113
title: str = item.get('title', '')
111-
resp = serialize_response_name(response, past_iine, title)
114+
115+
resp = serialize_response_name(response, new_iine, past_iine, title)
112116
return resp
113117

114118

@@ -120,7 +124,8 @@ def deserialize_response_name(response: Dict[str, Any], max_length=20) -> str:
120124
names = ", ".join(response.get('users', []))
121125
title = response.get('title', '')
122126
title = f"{title}" if len(title) <= max_length else f"{title[:max_length]}..."
123-
return f"\n{names}が「{title}」にいいねしました。"
127+
size = response.get('size', 0)
128+
return f"\nいいねが{size}回押されました。\n{names}が「{title}」にいいねしました。"
124129

125130

126131
def send_notification(message: str, token: str):

0 commit comments

Comments
 (0)