Last active
October 5, 2018 17:29
-
-
Save tribela/b05ed3960c8883b885aa13f0550c5d26 to your computer and use it in GitHub Desktop.
[Mastodon] Force redownload missing remote media
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import glob | |
import os | |
from os import path | |
import psycopg2 | |
DATA_DIR = '[MEDIA_PATH]/accounts' | |
URL = '[DATABASE_URI]' | |
conn = psycopg2.connect(URL) | |
def format_name(id_): | |
full = '{:09d}'.format(id_) | |
return '{}/{}/{}'.format( | |
full[:3], | |
full[3:6], | |
full[6:9] | |
) | |
def check_exists(id_): | |
full_path = path.join( | |
DATA_DIR, | |
format_name(id_) | |
) | |
return path.exists(full_path) | |
with conn.cursor() as cursor: | |
cursor.execute("select id, avatar_file_name, header_file_name from accounts") | |
rows = cursor.fetchall() | |
id_filename = {t[0]: t[1:] for t in rows} | |
for id_, filenames in id_filename.items(): | |
avatars = glob.glob(path.join(DATA_DIR, 'avatars', format_name(id_), '*/*')) | |
headers = glob.glob(path.join(DATA_DIR, 'headers', format_name(id_), '*/*')) | |
for avatar in avatars: | |
if path.splitext(path.basename(avatar))[0] != path.splitext(filenames[0])[0]: | |
# print('avatar', path.splitext(path.basename(avatar))[0], path.splitext(filenames[0])[0]) | |
print(avatar) | |
os.unlink(avatar) | |
for header in headers: | |
if path.splitext(path.basename(header))[0] != path.splitext(filenames[1])[0]: | |
# print('header', path.splitext(path.basename(header))[0], path.splitext(filenames[1])[0]) | |
print(header) | |
os.unlink(header) | |
conn.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from os import path | |
import psycopg2 | |
DATA_DIR = '/<CHANGE_THIS>/qdon/media_attachments/files' | |
URL = 'postgres://dbuser:dbpass@dbhost:5432/mastodon' | |
conn = psycopg2.connect(URL) | |
def format_name(id_): | |
full = '{:09d}'.format(id_) | |
return '{}/{}/{}'.format( | |
full[:3], | |
full[3:6], | |
full[6:9] | |
) | |
def check_exists(id_): | |
full_path = path.join( | |
DATA_DIR, | |
format_name(id_) | |
) | |
return path.exists(full_path) | |
with conn.cursor() as cursor: | |
cursor.execute("select id from media_attachments where remote_url != '' and file_file_name != ''") | |
rows = cursor.fetchall() | |
ids = list(map(lambda x: x[0], rows)) | |
missing_files = list(filter( | |
lambda x: not check_exists(x), | |
ids | |
)) | |
with conn.cursor() as cursor: | |
for id_ in missing_files: | |
cursor.execute( | |
"update media_attachments set file_file_name = '' where id = %s", | |
(id_,) | |
) | |
print(id_) | |
conn.commit() | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment