#!/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():
    """Downloads top `REDDIT_LIMIT` submissions from the /r/machinelearning subreddit.

    Uses Reddit API to get the hottest submissions. Expects `USER_AGENT`,
    `REDDIT_USERNAME`, and `REDDIT_PASS` in the function scope.

    Returns:
	list: List of relevant praw.orjects.Submission
    """


    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, min_comments=COMM_NUM_THRESHOLD):
    """Filters common HN and /r/machinelearning submissions

    The function calls the queries the Algolia API for each URL
    in the submissions provided and provides the HN IDs for submissions 
    that have more than `min_comments` comments,

    Args:
        reddit_submissions (iter): Iterable containing `praw` submission objects
        min_comments (int, optional) Minimum number of comments to consider a HN
            submission. The default value is taken from the module constant 
            `COMM_NUM_THRESHOLD`

    Returns:
        dict: A dict mapping `praw` submission objects to HN story IDs
    """


    # TODO Expand to loops for readability 
    return {reddit_sub:hit['objectID'] for reddit_sub in reddit_submissions
            for hit in requests.get(HN_ALGOLIA.format(reddit_sub.url)).json().get('hits', [])
            if hit['num_comments'] > COMM_NUM_THRESHOLD
            and urltools.compare(hit['url'], reddit_sub.url)}


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()

@@ -15,16 +15,297 @@
  python3
+%0A%22%22%22Reddit bot that links to HN submissions%0A%0AThe bot checks the top submissions on /r/machinelearning%0Aand compares the links against all HN submissions using the%0AAlgolia API. The bot posts a new top-level reply if there is %0Aa relatively active match and links to the HN thread.%0A%22%22%22
 %0A%0Aimport
@@ -669,20 +669,24 @@
 y to be 
-link
+consider
 ed%0ACOMM_
@@ -750,17 +750,16 @@
 Download
-s
  top %60RE
@@ -1021,16 +1021,17 @@
 bmission
+.
 %0A    %22%22%22
@@ -1388,17 +1388,16 @@
 %22%22Filter
-s
  common 
@@ -1433,16 +1433,17 @@
 missions
+.
 %0A%0A    Th
@@ -1457,18 +1457,8 @@
 ion 
-calls the 
 quer
@@ -1526,22 +1526,21 @@
 ded and 
-provid
+fetch
 es the H
@@ -1609,17 +1609,17 @@
 comments
-,
+.
 %0A%0A    Ar
@@ -1702,16 +1702,17 @@
  objects
+.
 %0A       
@@ -1894,16 +1894,17 @@
 RESHOLD%60
+.
 %0A%0A    Re
@@ -1980,16 +1980,17 @@
 tory IDs
+.
 %0A    %22%22%22
@@ -2358,16 +2358,211 @@
 _subs):%0A
+    %22%22%22Post comments on Reddit.%0A%0A    Posts %60len(common_subs)%60 comments every %60SLEEP_TIME%60 seconds. %0A%0A    Args:%0A        common_subs (dict): Maps %60praw%60 submission objects to HN story IDs.%0A    %22%22%22%0A
     for 

Add more documentation (close #1)