-
Notifications
You must be signed in to change notification settings - Fork 0
/
fmtv-downloader.py
171 lines (149 loc) · 6.65 KB
/
fmtv-downloader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import os
import time
import requests
import subprocess
import logging
import numpy as np
import ffmpeg
from logging.handlers import TimedRotatingFileHandler
from yt_dlp import YoutubeDL
# Environment variables
API_KEY = os.getenv('LASTFM_API_KEY', 'your_lastfm_api_key')
USERNAME = os.getenv('LASTFM_USERNAME', 'your_lastfm_username')
DOWNLOAD_PATH = os.getenv('DOWNLOAD_PATH', '/downloads')
APP_DATA_PATH = os.getenv('APP_DATA_PATH', '/appdata')
POLLING_INTERVAL = int(os.getenv('POLLING_INTERVAL', '300')) # Default to 300 seconds (5 minutes)
LASTFM_URL = f'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user={USERNAME}&api_key={API_KEY}&format=json'
LASTFM_TRACK_INFO_URL = f'http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key={API_KEY}&format=json&artist={{artist}}&track={{track}}'
# Configure logging
log_file_path = os.path.join(APP_DATA_PATH, 'downloader.log')
handler = TimedRotatingFileHandler(log_file_path, when="D", interval=7, backupCount=4)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.addHandler(handler)
def get_recent_tracks():
logger.info('Fetching recent tracks from Last.fm')
response = requests.get(LASTFM_URL)
if response.status_code == 200:
data = response.json()
recent_tracks = data['recenttracks']['track']
logger.info(f'Fetched {len(recent_tracks)} tracks')
return recent_tracks
else:
logger.error(f'Failed to fetch recent tracks: {response.status_code}')
return []
def get_track_info(artist, track):
url = LASTFM_TRACK_INFO_URL.format(artist=artist, track=track)
response = requests.get(url)
if response.status_code == 200:
data = response.json()
if 'track' in data:
track_info = data['track']
genre = track_info['toptags']['tag'][0]['name'] if track_info['toptags']['tag'] else ''
return genre
logger.error(f'Failed to fetch track info for {artist} - {track}: {response.status_code}')
return ''
def search_video(song_title, artist):
search_query = f'{artist} {song_title}'
ydl_opts = {
'format': 'best',
'noplaylist': True,
'quiet': True
}
try:
with YoutubeDL(ydl_opts) as ydl:
result = ydl.extract_info(f'ytsearch:{search_query}', download=False)['entries']
official_videos = [
entry for entry in result
if 'official' in entry['title'].lower()
]
remaster_videos = []
if not official_videos:
remaster_videos = [
entry for entry in result
if 'remaster' in entry['title'].lower() or 'remaster' in entry.get('description', '').lower()
]
if official_videos:
logging.info(f'Found official video: {official_videos[0]["title"]}')
return official_videos[0]['webpage_url']
elif remaster_videos:
logging.info(f'Found remastered video: {remaster_videos[0]["title"]}')
return remaster_videos[0]['webpage_url']
elif result:
logging.info(f'Found other video: {result[0]["title"]}')
return result[0]['webpage_url']
else:
logging.info('No videos found')
return None
except Exception as e:
logging.error(f'Error occurred: {e}')
return None
def download_song(video_url, song_title, artist, album, genre):
file_name = f'{artist} - {song_title}.mp4'
ydl_opts = {
'format': 'bestvideo+bestaudio/best',
'outtmpl': os.path.join(DOWNLOAD_PATH, file_name),
'merge_output_format': 'mp4'
}
with YoutubeDL(ydl_opts) as ydl:
ydl.download([video_url])
downloaded_file = os.path.join(DOWNLOAD_PATH, file_name)
# Check if the downloaded file is a static image
if is_static_image_for_5_seconds_ffmpeg(downloaded_file):
os.remove(downloaded_file)
logger.info(f'Deleted static image file: {downloaded_file}')
return
# Add metadata
tagged_file_name = f'{artist} - {song_title}_tagged.mp4'
ffmpeg_command = [
'ffmpeg',
'-i', downloaded_file,
'-metadata', f'title={song_title}',
'-metadata', f'artist={artist}',
'-metadata', f'album={album}',
'-metadata', f'genre={genre}',
'-codec', 'copy',
os.path.join(DOWNLOAD_PATH, tagged_file_name)
]
logger.info(f'Adding metadata for {song_title} by {artist}')
subprocess.run(ffmpeg_command, check=True)
# Replace the original file with the tagged file
os.rename(os.path.join(DOWNLOAD_PATH, tagged_file_name), downloaded_file)
logger.info(f'Successfully processed {song_title} by {artist}')
if __name__ == "__main__":
last_downloaded_track = None
while True:
try:
logger.info('Polling Last.fm for recent tracks')
recent_tracks = get_recent_tracks()
# Get the most recent track
if recent_tracks:
most_recent_track = recent_tracks[0]
song_title = most_recent_track['name']
artist = most_recent_track['artist']['#text']
album = most_recent_track.get('album', {}).get('#text', '')
# Check if the most recent track is different from the last downloaded track
if most_recent_track != last_downloaded_track:
last_downloaded_track = most_recent_track
# Check if the video file already exists
file_name = f'{artist} - {song_title}.mp4'
downloaded_file = os.path.join(DOWNLOAD_PATH, file_name)
if not os.path.exists(downloaded_file):
# Search and download the official video
video_url = search_video(song_title, artist)
if video_url:
logger.info(f'Found official video: {video_url}')
genre = get_track_info(artist, song_title)
download_song(video_url, song_title, artist, album, genre)
else:
logger.info('No official video found')
else:
logger.info(f'Video already downloaded: {downloaded_file}')
else:
logger.info('No recent tracks found')
time.sleep(POLLING_INTERVAL)
except Exception as e:
logger.error(f'An error occurred: {e}')
time.sleep(POLLING_INTERVAL)