Update GetMetadata.py

This commit is contained in:
afkarxyz authored and GitHub committed 2025-01-20 21:26:04 +07:00
1 parent f6a2986dbf
commit 72fb45fc8a
1 file changed
+48 -52
+48 -52
View File
@@ -1,75 +1,71 @@
import json import json
import subprocess import re
import sys
import os import os
from datetime import datetime from datetime import datetime
from gallery_dl.extractor import twitter
def get_metadata(username): def get_metadata(username):
if sys.platform == 'win32':
sys.stdout.reconfigure(encoding='utf-8')
url = f"https://x.com/{username}/timeline" url = f"https://x.com/{username}/timeline"
current_dir = os.path.dirname(os.path.abspath(__file__)) match = re.match(twitter.TwitterTimelineExtractor.pattern, url)
if not match:
raise ValueError(f"Invalid URL: {url}")
cookies_path = os.path.join(current_dir, 'cookies.json') extractor = twitter.TwitterTimelineExtractor(match)
output_path = os.path.join(current_dir, f'{username}.json')
gallery_dl_path = os.path.join(current_dir, 'gallery-dl.exe')
config = { extractor.config = lambda key, default=None: {
"extractor": { "cookies": {
"twitter": { "auth_token": ""
"cookies": {
"auth_token": ""
}
}
} }
} }.get(key, default)
with open(cookies_path, 'w') as f: extractor.initialize()
json.dump(config, f)
result = subprocess.run([gallery_dl_path, '-j', '--config', cookies_path, url],
capture_output=True,
text=True,
encoding='utf-8',
check=True)
os.remove(cookies_path)
data = json.loads(result.stdout)
output = { output = {
'account_info': {}, 'account_info': {},
'total_urls': 0,
'timeline': [] 'timeline': []
} }
for item in data: for item in extractor:
if isinstance(item, list) and len(item) > 2 and isinstance(item[2], dict): if isinstance(item, tuple) and len(item) >= 3:
if not output['account_info'] and 'user' in item[2]: media_url = item[1]
user = item[2]['user'] tweet_data = item[2]
output['account_info'] = {
'date': user.get('date', ''),
'followers_count': user.get('followers_count', ''),
'friends_count': user.get('friends_count', ''),
'name': user.get('name', ''),
'nick': user.get('nick', ''),
'profile_image': user.get('profile_image', ''),
'statuses_count': user.get('statuses_count', '')
}
if isinstance(item[1], str):
url = item[1]
if ('pbs.twimg.com' in url and 'format=jpg&name=orig' in url) or 'video.twimg.com' in url:
tweet_date = item[2].get('date')
if not tweet_date:
tweet_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
output['timeline'].append({ if not output['account_info']:
'url': url, if 'user' in tweet_data:
'date': tweet_date user = tweet_data['user']
}) user_date = user.get('date', '')
if isinstance(user_date, datetime):
user_date = user_date.strftime("%Y-%m-%d %H:%M:%S")
with open(output_path, 'w', encoding='utf-8') as f: output['account_info'] = {
'name': user.get('name', ''),
'nick': user.get('nick', ''),
'date': user_date,
'followers_count': user.get('followers_count', 0),
'friends_count': user.get('friends_count', 0),
'profile_image': user.get('profile_image', ''),
'statuses_count': user.get('statuses_count', 0)
}
if 'pbs.twimg.com' in media_url or 'video.twimg.com' in media_url:
tweet_date = tweet_data.get('date', datetime.now())
if isinstance(tweet_date, datetime):
tweet_date = tweet_date.strftime("%Y-%m-%d %H:%M:%S")
output['timeline'].append({
'url': media_url,
'date': tweet_date
})
output['total_urls'] += 1
current_dir = os.path.dirname(os.path.abspath(__file__))
output_file = os.path.join(current_dir, f"{username}.json")
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(output, f, ensure_ascii=False, indent=2) json.dump(output, f, ensure_ascii=False, indent=2)
return output
username = "" username = ""
get_metadata(username) get_metadata(username)