import praw
import os
from datetime import datetime
from getpass import getpass
from urllib.parse import urlparse

def download_saved_submissions(reddit_user, reddit_pass):
    userAgent = ('Saved Posts Downloader')
    reddit = praw.Reddit(user_agent=userAgent,
            client_id=os.environ['RED_SAVED_CLIENT_ID'],
            client_secret=os.environ['RED_SAVED_CLIENT_SECRET'],
            username=reddit_user,
            password=<PASSWORD>)
    return [saved for saved in reddit.user.me().saved() 
            if isinstance(saved, praw.models.Submission)]

def remove_reddit_submissions(submissions):
    return [submission for submission in submissions
            if urlparse(submission.url).hostname != 'www.reddit.com']

def write_to_bookmarks(bookmarks_path, submissions):
    f = open(f'{bookmarks_path}bookmarks.md', 'w')
    for submission in submissions:
        f.write(f'**{submission.title}**  \n')
        f.write(f'[{submission.url}]({submission.url})  \n')
        f.write('  \n')
    f.close()

def add_commit_push_bookmarks(repo_path):
    curr_dir = os.getcwd()
    os.chdir(repo_path)
    os.system('git add bookmarks.md')
    os.system(f'git commit -m "{str(datetime.now())}"')
    os.system('git push origin master')
    os.chdir(curr_dir)

def main():
    reddit_user = input('Enter reddit username: ')
    reddit_pass = getpass(f'Enter reddit password ({reddit_user}): ')
    submissions = download_saved_submissions(reddit_user, reddit_pass)
    submissions = remove_reddit_submissions(submissions)

    bookmarks_path = 'bookmarks/'
    write_to_bookmarks(bookmarks_path, submissions)

    add_commit_push_bookmarks(bookmarks_path)

if __name__ == '__main__':
    main()

import praw
import os
from datetime import datetime
from getpass import getpass
from urllib.parse import urlparse

def download_saved_submissions(reddit_user, reddit_pass):
    userAgent = ('Saved Posts Downloader')
    reddit = praw.Reddit(user_agent=userAgent,
            client_id=os.environ['RED_SAVED_CLIENT_ID'],
            client_secret=os.environ['RED_SAVED_CLIENT_SECRET'],
            username=reddit_user,
            password=<PASSWORD>)
    return [saved for saved in reddit.user.me().saved() 
            if isinstance(saved, praw.models.Submission)]

def remove_reddit_submissions(submissions):
    return [submission for submission in submissions
            if urlparse(submission.url).hostname != 'www.reddit.com']

def write_to_bookmarks(bookmarks_path, submissions):
    f = open(f'{bookmarks_path}bookmarks.md', 'w')
    for submission in submissions:
        f.write(f'**{submission.title}**  \n')
        f.write(f'[{submission.url}]({submission.url})  \n')
        f.write('  \n')
    f.close()

def add_commit_push_bookmarks(repo_path):
    curr_dir = os.getcwd()
    os.chdir(repo_path)
    os.system('git add bookmarks.md')
    os.system(f'git commit -m "{str(datetime.now())}"')
    os.system('git push origin master')
    os.chdir(curr_dir)

def main():
    reddit_user = input('Enter reddit username: ')
    reddit_pass = getpass(f'Enter reddit password ({reddit_user}): ')
    submissions = download_saved_submissions(reddit_user, reddit_pass)
    submissions = remove_reddit_submissions(submissions)

    bookmarks_path = 'bookmarks/'
    write_to_bookmarks(bookmarks_path, submissions)

    add_commit_push_bookmarks(bookmarks_path)

if __name__ == '__main__':
    main()
