Asks user on whether or not to delete submissions/comments before doing so.

This commit is contained in:
Joshua Merriman
2014-02-25 22:45:02 +11:00
parent f0751cf69d
commit bc14d722a6
+57 -49
View File
@@ -16,7 +16,6 @@ def print_dot():
sys.stdout.write('. ')
sys.stdout.flush()
def csv_file(fp, header):
"""Create or append a CSV file."""
if os.path.exists(fp):
@@ -29,10 +28,15 @@ def csv_file(fp, header):
return f, writer
def format_time(created):
return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(created))
# Ask the user for a decision. 'y' and 'Y' will evaluate to True, anything else will equate to False.
# A better implementation is welcome, it's just a quick-n-dirty slap-together job! -ChainsawPolice
def y_or_n(decision):
if decision.lower=='y': return True
else: return False
if __name__ == '__main__':
print '>> Login to OLD account..'
@@ -43,56 +47,60 @@ if __name__ == '__main__':
print '\t>>Login successful..'
old_user = old_r.user # get a praw.objects.LoggedInRedditor object
print '>> Saving and editing all comments...'
print 'Would you like to remove all your old comments? (y/n)'
if y_or_n(raw_input('> ')) == True:
print '>> Saving and editing all comments...'
comment_file, comment_csv = csv_file(
'{}_comments.csv'.format(old_user.name),
['Comment', "Posted on", "Thread"])
comment_file, comment_csv = csv_file(
'{}_comments.csv'.format(old_user.name),
['Comment', "Posted on", "Thread"])
with comment_file:
removed = 1
while removed > 0: # keep going until everything is gone
removed = 0
for com in old_user.get_comments(limit=None):
link = com.submission.permalink.encode('utf-8')
body = com.body.encode('utf-8')
row = [body, format_time(com.created), link]
try:
comment_csv.writerow(row)
com.edit('.')
removed += 1
print_dot()
except Exception as e:
print 'Failed to store', link
print e
print '\n\t>> Saved to {0}_comments.csv'.format(old_user.name)
with comment_file:
removed = 1
while removed > 0: # keep going until everything is gone
removed = 0
for com in old_user.get_comments(limit=None):
link = com.submission.permalink.encode('utf-8')
body = com.body.encode('utf-8')
row = [body, format_time(com.created), link]
try:
comment_csv.writerow(row)
com.edit('.')
removed += 1
print_dot()
except Exception as e:
print 'Failed to store', link
print e
print '\n\t>> Saved to {0}_comments.csv'.format(old_user.name)
print '>> Saving and editing all submissions...'
submission_header = ['Title', "Body/Link", "Created", "Karma"]
submission_file, submission_csv = csv_file(
'{}_submissions.csv'.format(old_user.name),
submission_header)
print 'Would you like to remove all your old submissions? (y/n)'
if y_or_n(raw_input('> ')) == True:
print '>> Saving and editing all submissions...'
submission_header = ['Title', "Body/Link", "Created", "Karma"]
submission_file, submission_csv = csv_file(
'{}_submissions.csv'.format(old_user.name),
submission_header)
with submission_file:
removed = 1
while removed > 0: # keep going until everything is gone
removed = 0
for sub in old_user.get_submitted(limit=None):
if sub.is_self:
submission = sub.selftext.encode('utf-8')
else:
submission = sub.url.encode('utf-8')
title = sub.title.encode('utf-8')
row = [title, submission, format_time(sub.created), sub.score]
try:
submission_csv.writerow(row)
sub.edit('.')
removed += 1
print_dot()
except Exception as e:
print 'Failed to store', submission
print e
print '\n\t>> Saved to {0}_submissions.csv'.format(old_user.name)
with submission_file:
removed = 1
while removed > 0: # keep going until everything is gone
removed = 0
for sub in old_user.get_submitted(limit=None):
if sub.is_self:
submission = sub.selftext.encode('utf-8')
else:
submission = sub.url.encode('utf-8')
title = sub.title.encode('utf-8')
row = [title, submission, format_time(sub.created), sub.score]
try:
submission_csv.writerow(row)
sub.edit('.')
removed += 1
print_dot()
except Exception as e:
print 'Failed to store', submission
print e
print '\n\t>> Saved to {0}_submissions.csv'.format(old_user.name)
print '>> Preparing to migrate subscriptions.'
subs = old_r.get_my_subreddits(limit=None)
@@ -126,4 +134,4 @@ if __name__ == '__main__':
print '\n\t>> Done migrating.'
print '>> Go to https://ssl.reddit.com/prefs/delete/',
print 'to delete your old account.'
print 'to delete your old account.'