# Path: channels/app.py
# def init_database(app):
# def init_disqus(app):
# def init_publisher(app):
# def get_client(key):


import cPickle as pickle
import datetime
import simplejson as json
import urllib
from channels.app import app, disqusapi
from disqusapi import Paginator

TALK_CATEGORY_NAME = 'Talk'
talk_category_id = False


def api_args(**kwargs):
    base = dict(
        forum=app.config['DISQUS_FORUM'],
        access_token=app.config['DISQUS_ACCESS_TOKEN']
    )
    return dict(base.items() + kwargs.items())


def api(method, *args, **kwargs):
    return method(*args, **api_args(**kwargs))


print 'Trying to find %s category in existing categories...' % TALK_CATEGORY_NAME
for existing_category in api(disqusapi.categories.list):
    if existing_category['title'] == TALK_CATEGORY_NAME:
        talk_category_id = existing_category['id']

if not talk_category_id:
    print "Couldn't find it, adding it..."
    new_category = api(disqusapi.categories.create, title=TALK_CATEGORY_NAME)
    talk_category_id = new_category['id']


print "Downloading list of existing threads..."

# Path: channels/app.py
# def init_database(app):
# def init_disqus(app):
# def init_publisher(app):
# def get_client(key):


# Path: setup_threads.py
import cPickle as pickle
import datetime
import simplejson as json
import urllib
from channels.app import app, disqusapi
from disqusapi import Paginator


TALK_CATEGORY_NAME = 'Talk'
talk_category_id = False


def api_args(**kwargs):
    base = dict(
        forum=app.config['DISQUS_FORUM'],
        access_token=app.config['DISQUS_ACCESS_TOKEN']
    )
    return dict(base.items() + kwargs.items())


def api(method, *args, **kwargs):
    return method(*args, **api_args(**kwargs))


print 'Trying to find %s category in existing categories...' % TALK_CATEGORY_NAME
for existing_category in api(disqusapi.categories.list):
    if existing_category['title'] == TALK_CATEGORY_NAME:
        talk_category_id = existing_category['id']

if not talk_category_id:
    print "Couldn't find it, adding it..."
    new_category = api(disqusapi.categories.create, title=TALK_CATEGORY_NAME)
    talk_category_id = new_category['id']


print "Downloading list of existing threads..."

paginator = Paginator(disqusapi.threads.list, **api_args(category=talk_category_id))