Skip to content

Instantly share code, notes, and snippets.

@cazzerson
Last active February 21, 2017 16:05
Show Gist options
  • Save cazzerson/ca92fac571f07d8150edbe43cd2a9b08 to your computer and use it in GitHub Desktop.
Save cazzerson/ca92fac571f07d8150edbe43cd2a9b08 to your computer and use it in GitHub Desktop.
A quick management command to export all user tweets from the old Social Feed Manager. This may require an older version of openpyxl if you've updated the packages recently. Place this file in `sfm/ui/management/commands/`, create an `sfm/exports` directory, then: `cd sfm` and `./manage.py export-all-users`. Then look in the `exports` directory.
import codecs
from optparse import make_option
import sys
from django.core.management.base import BaseCommand, CommandError
from ui.models import TwitterUser, TwitterUserSet, TwitterUserItem
from ui.utils import make_date_aware, xls_tweets_workbook
import os
class Command(BaseCommand):
help = 'export data for a user or a set in csv, xls or json'
option_list = BaseCommand.option_list + (
make_option('--format', action='store', default='json',
type='string', dest='format',
help='set output format (csv, xls, json)'),
)
def handle(self, *args, **options):
twitter_user = user_set = fmt = filename = None
fmt = options['format'].lower()
if fmt not in ['csv', 'json', 'xls']:
fmt = 'json'
qs_tweeps = TwitterUser.objects.all()
for tweep in qs_tweeps:
filename = "export/%s_%s/%s_%s.%s" % (tweep.uid, tweep.name, tweep.uid, tweep.name, fmt)
try:
twitter_user = TwitterUser.objects.get(
name__iexact=tweep.name)
qs = twitter_user.items.all()
except TwitterUser.DoesNotExist:
raise CommandError('TwitterUser %s does not exist' %
tweep.name)
# tweak for python 2.7 to avoid having to set PYTHONIOENCODING=utf8
# in environment, see Graham Fawcett's comment/suggestion at:
# nedbatchelder.com/blog/200401/printing_unicode_from_python.html
if filename:
if not os.path.exists(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))
sys.stdout = codecs.open(filename, 'w', 'utf-8')
else:
writer_class = codecs.getwriter('utf-8')
sys.stdout = writer_class(sys.stdout, 'replace')
if fmt == 'xls':
tworkbook = xls_tweets_workbook(qs, TwitterUserItem.csv_headers)
tworkbook.save(filename)
if fmt == 'json':
for tui in qs:
print tui.item_json
else:
print '\t'.join(TwitterUserItem.csv_headers)
for tui in qs:
print '\t'.join(tui.csv)
@lwrubel
Copy link

lwrubel commented Feb 10, 2017

thanks, @cazzerson!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment