From 1b1cf01d0df71a0bc7a292d3c8e13d2f52521742 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Thu, 15 Oct 2020 00:43:26 +0200 Subject: [PATCH] add a general 'generate_csrf_token()' function --- gallery_dl/extractor/twitter.py | 6 ++---- gallery_dl/util.py | 5 +++++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/gallery_dl/extractor/twitter.py b/gallery_dl/extractor/twitter.py index c98a300e..a922a6c5 100644 --- a/gallery_dl/extractor/twitter.py +++ b/gallery_dl/extractor/twitter.py @@ -9,10 +9,8 @@ """Extractors for https://twitter.com/""" from .common import Extractor, Message -from .. import text, exception +from .. import text, util, exception from ..cache import cache -import hashlib -import time BASE_PATTERN = ( @@ -446,7 +444,7 @@ class TwitterAPI(): cookies = self.extractor.session.cookies # CSRF - csrf = hashlib.md5(str(time.time()).encode()).hexdigest() + csrf = util.generate_csrf_token() self.headers["x-csrf-token"] = csrf cookies.set("ct0", csrf, domain=".twitter.com") diff --git a/gallery_dl/util.py b/gallery_dl/util.py index 3e91405d..f3fc483d 100644 --- a/gallery_dl/util.py +++ b/gallery_dl/util.py @@ -16,6 +16,7 @@ import time import shutil import string import _string +import hashlib import sqlite3 import datetime import operator @@ -60,6 +61,10 @@ def raises(cls): return wrap +def generate_csrf_token(): + return hashlib.md5(str(time.time()).encode()).hexdigest() + + def combine_dict(a, b): """Recursively combine the contents of 'b' into 'a'""" for key, value in b.items():