#!/usr/bin/env python3

import time
import collections

import praw
import requests
import urltools

from config import REDDIT_USERNAME, REDDIT_PASS, USER_AGENT

REDDIT_LIMIT = 50

HN_ALGOLIA = 'http://hn.algolia.com/api/v1/search?query={}&restrictSearchableAttributes=url' 
HN_STORY = 'https://news.ycombinator.com/item?id={}'

SLEEP_TIME = 60
# Minimum number of comments of an HN story to be linked
COMM_NUM_THRESHOLD = 3


def get_reddit_submissions():
    r = praw.Reddit(user_agent=USER_AGENT)
    r.login(REDDIT_USERNAME, REDDIT_PASS, disable_warning=True)
    submissions = r.get_subreddit('machinelearning').get_hot(limit=REDDIT_LIMIT)
    return [sub for sub in submissions if 'reddit.com' not in sub.url]


def get_common_submissions(reddit_submissions):
    commons = collections.defaultdict(list)
    for sub in reddit_submissions:
        hn_hits = requests.get(HN_ALGOLIA.format(sub.url)).json().get('hits')
        if hn_hits:
            for hit in hn_hits:
                if (hit['num_comments'] > COMM_NUM_THRESHOLD
                    and urltools.compare(hit['url'], sub.url)):
                    commons[sub].append(hit['objectID'])
    return commons


def post_comments(common_subs):
    for reddit_obj, hn_ids in common_subs.items():
	# If I didn't post here before
        if not any(comm for comm in reddit_obj.comments if REDDIT_USERNAME in str(comm.author)):
            hn_urls = [HN_STORY.format(hn_id) for hn_id in hn_ids]
            reddit_obj.add_comment('HN discussion: {}'.format('\n'.join(hn_urls)))
            time.sleep(SLEEP_TIME)

def main():
    reddit_subs = get_reddit_submissions()
    common_subs = get_common_submissions(reddit_subs)
    post_comments(common_subs)

if __name__ == '__main__':
    main()

@@ -446,24 +446,322 @@
 missions():%0A
+    %22%22%22Downloads top %60REDDIT_LIMIT%60 submissions from the /r/machinelearning subreddit.%0A%0A    Uses Reddit API to get the hottest submissions. Expects %60USER_AGENT%60,%0A    %60REDDIT_USERNAME%60, and %60REDDIT_PASS%60 in the function scope.%0A%0A    Returns:%0A%09list: List of relevant praw.orjects.Submission%0A    %22%22%22%0A%0A%0A
     r = praw
@@ -1058,224 +1058,883 @@
 ions
-):%0A    commons = collections.defaultdict(list)%0A    for sub in reddit_submissions:%0A        hn_hits = requests.get(HN_ALGOLIA.format(sub.url)).json().get('hits')%0A        if hn_hits:%0A            for hit in hn_hits:%0A    
+, min_comments=COMM_NUM_THRESHOLD):%0A    %22%22%22Filters common HN and /r/machinelearning submissions%0A%0A    The function calls the queries the Algolia API for each URL%0A    in the submissions provided and provides the HN IDs for submissions %0A    that have more than %60min_comments%60 comments,%0A%0A    Args:%0A        reddit_submissions (iter): Iterable containing %60praw%60 submission objects%0A        min_comments (int, optional) Minimum number of comments to consider a HN%0A            submission. The default value is taken from the module constant %0A            %60COMM_NUM_THRESHOLD%60%0A%0A    Returns:%0A        dict: A dict mapping %60praw%60 submission objects to HN story IDs%0A    %22%22%22%0A%0A%0A    # TODO Expand to loops for readability %0A    return %7Breddit_sub:hit%5B'objectID'%5D for reddit_sub in reddit_submissions%0A            for hit in requests.get(HN_ALGOLIA.format(reddit_sub.url)).json().get('hits', %5B%5D)%0A
     
@@ -1944,17 +1944,16 @@
      if 
-(
 hit%5B'num
@@ -1997,24 +1997,16 @@
         
-   
-     
 and urlt
@@ -2034,94 +2034,24 @@
 '%5D, 
-sub.url)):%0A                    commons%5Bsub%5D.append(hit%5B'objectID'%5D)%0A    return commons
+reddit_sub.url)%7D
 %0A%0A%0Ad

Add a few function docstrings (addresses #1)