Skip to content

Commit 91f8cbe

Browse files
committed
Improved twitter card script so that I can run it in cron
1 parent 9dbcd90 commit 91f8cbe

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

dbdb/core/management/commands/create_twitter_cards.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# stdlib imports
22
import os
3+
import sys
34

45
# django imports
56
from django.core.management import BaseCommand
@@ -16,29 +17,41 @@
1617
class Command(BaseCommand):
1718

1819
def add_arguments(self, parser):
19-
parser.add_argument('--system', type=str)
20-
parser.add_argument('--missing', action='store_true')
20+
parser.add_argument('system', metavar='S', type=str, nargs='?',
21+
help='System to force twiter card creation')
2122
return
2223

2324
def handle(self, *args, **options):
2425
template = os.path.join(settings.BASE_DIR, "static", settings.TWITTER_CARD_TEMPLATE)
2526
assert os.path.exists(template), "Missing: " + template
2627

2728
versions = SystemVersion.objects.filter(is_current=True)
29+
force = False
2830
if options['system']:
2931
keyword = options['system']
30-
3132
if keyword.isdigit():
3233
versions = versions.filter(system__id=int(keyword))
3334
else:
3435
versions = versions.filter(system__name__icontains=keyword)
36+
force = True
3537

3638
for ver in versions:
3739
card_img = os.path.join(settings.TWITTER_CARD_ROOT, ver.get_twitter_card_image())
38-
if options['missing'] and os.path.exists(card_img): continue
40+
logo_img = os.path.join(settings.MEDIA_ROOT, ver.logo.name)
41+
42+
# Only create ones that are missing or where the logo
43+
# is newer than the existing twitter image
44+
if not force:
45+
if os.path.exists(card_img) and os.path.exists(logo_img):
46+
if os.path.getmtime(logo_img) <= os.path.getmtime(card_img):
47+
#print(logo_img, "=>", os.path.getmtime(logo_img))
48+
#print(card_img, "=>", os.path.getmtime(card_img))
49+
#self.stdout.write("SKIP: %s -> %s" % (ver.system.name, card_img))
50+
continue
51+
elif os.path.exists(card_img):
52+
continue
3953
ver.create_twitter_card()
4054
self.stdout.write("%s -> %s" % (ver.system.name, card_img))
41-
4255
# FOR
4356
return
4457

0 commit comments

Comments
 (0)