mirror of
https://github.com/Alvin-Zilverstand/Twitter-X-Media-Batch-Downloader.git
synced 2026-11-24 15:23:11 +01:00
Update GetMetadata.py
This commit is contained in:
1 parent
f6a2986dbf
commit
72fb45fc8a
1 file changed
+50
-54
+50
-54
@@ -1,75 +1,71 @@
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
import re
|
||||
import os
|
||||
from datetime import datetime
|
||||
from gallery_dl.extractor import twitter
|
||||
|
||||
def get_metadata(username):
|
||||
if sys.platform == 'win32':
|
||||
sys.stdout.reconfigure(encoding='utf-8')
|
||||
|
||||
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}")
|
||||
|
||||
extractor = twitter.TwitterTimelineExtractor(match)
|
||||
|
||||
cookies_path = os.path.join(current_dir, 'cookies.json')
|
||||
output_path = os.path.join(current_dir, f'{username}.json')
|
||||
gallery_dl_path = os.path.join(current_dir, 'gallery-dl.exe')
|
||||
|
||||
config = {
|
||||
"extractor": {
|
||||
"twitter": {
|
||||
"cookies": {
|
||||
"auth_token": ""
|
||||
}
|
||||
}
|
||||
extractor.config = lambda key, default=None: {
|
||||
"cookies": {
|
||||
"auth_token": ""
|
||||
}
|
||||
}
|
||||
}.get(key, default)
|
||||
|
||||
with open(cookies_path, 'w') as f:
|
||||
json.dump(config, f)
|
||||
extractor.initialize()
|
||||
|
||||
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 = {
|
||||
'account_info': {},
|
||||
'total_urls': 0,
|
||||
'timeline': []
|
||||
}
|
||||
|
||||
for item in data:
|
||||
if isinstance(item, list) and len(item) > 2 and isinstance(item[2], dict):
|
||||
if not output['account_info'] and 'user' in item[2]:
|
||||
user = item[2]['user']
|
||||
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({
|
||||
'url': url,
|
||||
'date': tweet_date
|
||||
})
|
||||
for item in extractor:
|
||||
if isinstance(item, tuple) and len(item) >= 3:
|
||||
media_url = item[1]
|
||||
tweet_data = item[2]
|
||||
|
||||
if not output['account_info']:
|
||||
if 'user' in tweet_data:
|
||||
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")
|
||||
|
||||
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_path, 'w', encoding='utf-8') as f:
|
||||
with open(output_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(output, f, ensure_ascii=False, indent=2)
|
||||
|
||||
return output
|
||||
|
||||
username = ""
|
||||
get_metadata(username)
|
||||
Reference in new issue
Block a user