사용하지 않는 코드 정리 및 전송 데이터 포멧팅 변경
This commit is contained in:
1 parent
a0c352f567
commit
cb267192d2
5 files changed
+137
-208
No files matched your search
@@ -204,64 +204,64 @@ class DataProcessor:
|
||||
ContentMetric(
|
||||
id="total-views",
|
||||
label="조회수",
|
||||
label_en="Total Views",
|
||||
value=self._format_number(int(views)),
|
||||
value=float(views),
|
||||
unit="count",
|
||||
trend=round(float(views_trend), 1),
|
||||
trend_direction=views_dir,
|
||||
),
|
||||
ContentMetric(
|
||||
id="total-watch-time",
|
||||
label="시청시간",
|
||||
label_en="Watch Time",
|
||||
value=f"{round(estimated_minutes_watched / 60, 1)}시간",
|
||||
value=round(estimated_minutes_watched / 60, 1),
|
||||
unit="hours",
|
||||
trend=round(watch_trend / 60, 1),
|
||||
trend_direction=watch_dir,
|
||||
),
|
||||
ContentMetric(
|
||||
id="avg-view-duration",
|
||||
label="평균 시청시간",
|
||||
label_en="Avg. View Duration",
|
||||
value=f"{round(average_view_duration / 60)}분",
|
||||
value=round(average_view_duration / 60, 1),
|
||||
unit="minutes",
|
||||
trend=round(duration_trend / 60, 1),
|
||||
trend_direction=duration_dir,
|
||||
),
|
||||
ContentMetric(
|
||||
id="new-subscribers",
|
||||
label="신규 구독자",
|
||||
label_en="New Subscribers",
|
||||
value=self._format_number(int(subscribers_gained)),
|
||||
trend=round(float(subs_trend), 1),
|
||||
value=float(subscribers_gained),
|
||||
unit="count",
|
||||
trend=subs_trend,
|
||||
trend_direction=subs_dir,
|
||||
),
|
||||
ContentMetric(
|
||||
id="likes",
|
||||
label="좋아요",
|
||||
label_en="Likes",
|
||||
value=self._format_number(int(likes)),
|
||||
trend=round(float(likes_trend), 1),
|
||||
value=float(likes),
|
||||
unit="count",
|
||||
trend=likes_trend,
|
||||
trend_direction=likes_dir,
|
||||
),
|
||||
ContentMetric(
|
||||
id="comments",
|
||||
label="댓글",
|
||||
label_en="Comments",
|
||||
value=self._format_number(int(comments)),
|
||||
trend=round(float(comments_trend), 1),
|
||||
value=float(comments),
|
||||
unit="count",
|
||||
trend=comments_trend,
|
||||
trend_direction=comments_dir,
|
||||
),
|
||||
ContentMetric(
|
||||
id="shares",
|
||||
label="공유",
|
||||
label_en="Shares",
|
||||
value=self._format_number(int(shares)),
|
||||
trend=round(float(shares_trend), 1),
|
||||
value=float(shares),
|
||||
unit="count",
|
||||
trend=shares_trend,
|
||||
trend_direction=shares_dir,
|
||||
),
|
||||
ContentMetric(
|
||||
id="uploaded-videos",
|
||||
label="업로드 영상",
|
||||
label_en="Uploaded Videos",
|
||||
value=str(period_video_count),
|
||||
value=float(period_video_count),
|
||||
unit="count",
|
||||
trend=0.0,
|
||||
trend_direction="-",
|
||||
),
|
||||
@@ -475,53 +475,6 @@ class DataProcessor:
|
||||
top_regions=top_regions,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _format_number(num: int) -> str:
|
||||
"""숫자 포맷팅 (1234567 → "1.2M")
|
||||
|
||||
조회수, 구독자 수 등 큰 숫자를 읽기 쉽게 포맷팅합니다.
|
||||
|
||||
Args:
|
||||
num: 원본 숫자
|
||||
|
||||
Returns:
|
||||
str: 포맷팅된 문자열
|
||||
- 1,000,000 이상: "1.2M"
|
||||
- 1,000 이상: "12.5K"
|
||||
- 1,000 미만: "123"
|
||||
|
||||
Example:
|
||||
>>> _format_number(1234567)
|
||||
"1.2M"
|
||||
>>> _format_number(12345)
|
||||
"12.3K"
|
||||
>>> _format_number(123)
|
||||
"123"
|
||||
"""
|
||||
if num >= 1_000_000:
|
||||
return f"{num / 1_000_000:.1f}M"
|
||||
elif num >= 1_000:
|
||||
return f"{num / 1_000:.1f}K"
|
||||
else:
|
||||
return str(num)
|
||||
|
||||
@staticmethod
|
||||
def _format_duration(seconds: int) -> str:
|
||||
"""초를 M:SS 형식으로 변환 (평균 시청 시간 표시용)
|
||||
|
||||
Args:
|
||||
seconds: 초 단위 시간
|
||||
|
||||
Returns:
|
||||
str: M:SS 형식 문자열
|
||||
- 204초 → "3:24"
|
||||
- 65초 → "1:05"
|
||||
- 45초 → "0:45"
|
||||
"""
|
||||
minutes = seconds // 60
|
||||
secs = seconds % 60
|
||||
return f"{minutes}:{secs:02d}"
|
||||
|
||||
@staticmethod
|
||||
def _translate_country_code(code: str) -> str:
|
||||
"""국가 코드를 한국어로 변환
|
||||
|
||||
@@ -44,19 +44,22 @@ class YouTubeAnalyticsService:
|
||||
"""YouTube Analytics API 호출을 병렬로 실행
|
||||
|
||||
Args:
|
||||
video_ids: YouTube 영상 ID 리스트 (최대 30개)
|
||||
video_ids: YouTube 영상 ID 리스트 (최대 30개, 빈 리스트 허용)
|
||||
start_date: 조회 시작일 (YYYY-MM-DD)
|
||||
end_date: 조회 종료일 (YYYY-MM-DD)
|
||||
access_token: YouTube OAuth 2.0 액세스 토큰
|
||||
mode: 조회 모드 ("month" | "day")
|
||||
kpi_end_date: KPI 집계 종료일 (미전달 시 end_date와 동일)
|
||||
|
||||
Returns:
|
||||
dict[str, Any]: API 응답 데이터
|
||||
- kpi: KPI 메트릭 (조회수, 좋아요, 댓글 등)
|
||||
- monthly_recent: 최근 12개월 월별 조회수
|
||||
- monthly_previous: 이전 12개월 월별 조회수
|
||||
- top_videos: 인기 영상 TOP 4
|
||||
- demographics: 연령/성별 분포
|
||||
- region: 지역별 조회수
|
||||
dict[str, Any]: API 응답 데이터 (7개 키)
|
||||
- kpi: 최근 기간 KPI 메트릭 (조회수, 좋아요, 댓글 등)
|
||||
- kpi_previous: 이전 기간 KPI 메트릭 (trend 계산용)
|
||||
- trend_recent: 최근 기간 추이 (월별 또는 일별 조회수)
|
||||
- trend_previous: 이전 기간 추이 (전년 또는 이전 30일)
|
||||
- top_videos: 조회수 기준 인기 영상 TOP 4
|
||||
- demographics: 연령/성별 시청자 분포
|
||||
- region: 지역별 조회수 TOP 5
|
||||
|
||||
Raises:
|
||||
YouTubeAPIError: API 호출 실패
|
||||
@@ -73,7 +76,7 @@ class YouTubeAnalyticsService:
|
||||
... )
|
||||
"""
|
||||
logger.debug(
|
||||
f"[1/6] YouTube Analytics API 병렬 호출 시작 - "
|
||||
f"[1/7] YouTube Analytics API 병렬 호출 시작 - "
|
||||
f"video_count={len(video_ids)}, period={start_date}~{end_date}, mode={mode}"
|
||||
)
|
||||
|
||||
@@ -255,7 +258,7 @@ class YouTubeAnalyticsService:
|
||||
"endDate": end_date,
|
||||
"dimensions": "month",
|
||||
"metrics": "views",
|
||||
# "filters": f"video=={','.join(video_ids)}",
|
||||
"filters": f"video=={','.join(video_ids)}",
|
||||
"sort": "month",
|
||||
}
|
||||
|
||||
@@ -299,7 +302,7 @@ class YouTubeAnalyticsService:
|
||||
"endDate": end_date,
|
||||
"dimensions": "day",
|
||||
"metrics": "views",
|
||||
# "filters": f"video=={','.join(video_ids)}",
|
||||
"filters": f"video=={','.join(video_ids)}",
|
||||
"sort": "day",
|
||||
}
|
||||
|
||||
@@ -340,7 +343,7 @@ class YouTubeAnalyticsService:
|
||||
"endDate": end_date,
|
||||
"dimensions": "video",
|
||||
"metrics": "views,likes,comments",
|
||||
# "filters": f"video=={','.join(video_ids)}",
|
||||
"filters": f"video=={','.join(video_ids)}",
|
||||
"sort": "-views",
|
||||
"maxResults": "4",
|
||||
}
|
||||
@@ -410,7 +413,7 @@ class YouTubeAnalyticsService:
|
||||
rows = [["KR", 1000000], ["US", 500000], ...]
|
||||
조회수 내림차순으로 정렬된 상위 5개 국가
|
||||
"""
|
||||
logger.debug("[YouTubeAnalyticsService._fetch_country] START")
|
||||
logger.debug("[YouTubeAnalyticsService._fetch_region] START")
|
||||
|
||||
params = {
|
||||
"ids": "channel==MINE",
|
||||
@@ -418,7 +421,7 @@ class YouTubeAnalyticsService:
|
||||
"endDate": end_date,
|
||||
"dimensions": "country",
|
||||
"metrics": "views",
|
||||
# "filters": f"video=={','.join(video_ids)}",
|
||||
"filters": f"video=={','.join(video_ids)}",
|
||||
"sort": "-views",
|
||||
"maxResults": "5",
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user