def get_forums_list(account_id): public_key = settings.CONNECTED_ACCOUNTS_DISQUS_CONSUMER_KEY secret_key = settings.CONNECTED_ACCOUNTS_DISQUS_CONSUMER_SECRET disqus = DisqusAPI(secret_key=secret_key, public_key=public_key) forums = [] try: connected_account = Account.objects.get(pk=account_id) except Account.DoesNotExist: raise DisqusAPIError(_('Account does not exist!')) else: try: forums_list = disqus.users.listForums( access_token=connected_account.get_token()) except (APIError, InvalidAccessToken) as e: raise DisqusAPIError(e.message) else: for forum in forums_list: shortname = forum['id'] label_from_instance = '{name} ({shortname})'.format( name=forum['name'], shortname=shortname) forums.append((shortname, label_from_instance)) return forums
def disqus_static(generator): disqus = DisqusAPI(generator.settings['DISQUS_SECRET_KEY'], generator.settings['DISQUS_PUBLIC_KEY']) # first retrieve the threads threads = Paginator(disqus.threads.list, forum=generator.settings['DISQUS_SITENAME']) # build a {thread_id: title} dict thread_dict = {} for thread in threads: thread_dict[thread['id']] = thread['title'] # now retrieve the posts posts = Paginator(disqus.posts.list, forum=generator.settings['DISQUS_SITENAME']) # build a {post_id: [child_post1, child_post2, ...]} dict child_dict = {} for post in posts: if post['id'] not in child_dict.keys(): child_dict[post['id']] = [] if post['parent'] is not None: if str(post['parent']) not in child_dict.keys(): child_dict[str(post['parent'])] = [] child_dict[str(post['parent'])].append(post) # build a {title: [post1, post2, ...]} dict post_dict = {} for post in posts: build_post_dict(post_dict, child_dict, thread_dict, post) for article in generator.articles: if article.title in post_dict: article.disqus_comments = post_dict[article.title] article.disqus_comment_count = sum( [postcounter(post) for post in post_dict[article.title]])
def get(self): if not self.current_user_can('manage_disqus'): return self.write("not authorized") from disqusapi import DisqusAPI disqus = DisqusAPI(settings.get('disqus_secret_key'), settings.get('disqus_public_key')) for result in disqus.trends.listThreads(): self.write(result)
def write_comments_api(posts): disqus = DisqusAPI( os.environ.get('DISQUS_SECRET'), os.environ.get('DISQUS_APIKEY'), ) access_token = os.environ.get('DISQUS_ACCESS_TOKEN') for id, post in posts.items(): if not post.get("comments"): continue thread_id = None title = post.get("title") date_added = post.get("date") url = "http://chase-seibert.github.com/blog/%s/%02d/%02d/%s.html" % ( date_added.year, date_added.month, date_added.day, slugify(title)) print url try: thread = disqus.threads.create( access_token=access_token, method="POST", forum="chaseseibertblog", title=title, url=url) print "Created thread: " + thread.get("id") thread_id = thread.get("id") except APIError: # already exists thread = disqus.threads.list( access_token=access_token, forum="chaseseibertblog", thread="link:%s" % url, )[0] print "Thread already exists: " + thread.get("id") thread_id = thread.get("id") for comment in post.get("comments", []): try: new_comment = disqus.posts.create( access_token=access_token, method="POST", thread=thread_id, message=comment.get("message"), #author_name=comment.get("author"), #author_email="*****@*****.**", date=time.mktime(comment.get("date").timetuple())) print "Created comment: " + new_comment.get("id") except UnicodeEncodeError: pass
def getDQComments(self): # http://disqus.com/api/docs/posts/list/ # https://github.com/disqus/disqus-python from disqusapi import DisqusAPI from mezzanine.conf import settings settings.use_editable() disqus = DisqusAPI(settings.COMMENTS_DISQUS_API_SECRET_KEY, settings.COMMENTS_DISQUS_API_PUBLIC_KEY) posts = disqus.forums.listPosts(forum='digipal') for post in posts: print post print posts
def to_representation(self, instance): try: DISQUS = DisqusAPI(settings.DISQUS_SECRET_KEY, settings.DISQUS_PUBLIC_KEY) thread = 'link:%s' % settings.FORUM_URL_REGEX % ( settings.SITE_DOMAIN, instance.competition.slug, instance.slug) instance.disqus_posts = DISQUS.threads.listPosts( method='GET', forum=settings.DISQUS_SHORTNAME, thread=thread, limit=1 ) instance.disqus_details = DISQUS.threads.details( method='GET', forum=settings.DISQUS_SHORTNAME, thread=thread ) except Exception as e: instance.disqus_posts = {} instance.disqus_details = {} return super(ForumTopicListSerializer, self).to_representation(instance)
def test_setKey(self): api = DisqusAPI('a') self.assertEquals(api.key, 'a') api.setKey('b') self.assertEquals(api.key, 'b')
def test_users_listActivity(self): api = DisqusAPI(self.API_SECRET) self.assertRaises(APIError, api.users.listActivity, foo='bar')
def test_setVersion(self): api = DisqusAPI() self.assertEquals(api.version, '3.0') api.setVersion('3.1') self.assertEquals(api.version, '3.1')
def test_setFormat(self): api = DisqusAPI() self.assertEquals(api.format, 'json') api.setFormat('jsonp') self.assertEquals(api.format, 'jsonp')
import json import os from disqusapi import DisqusAPI, Paginator disqus = DisqusAPI(secret_key="KEY_HERE", public_key="KEY_HERE") p = Paginator( disqus.api, "users.listPosts", user="******", limit="100", order="asc", method="GET", ) for post in p: try: filename = "comments/{0[createdAt]}_{0[id]}.json".format(post) with open(filename, "x") as f: json.dump(post, f) except FileExistsError: continue for f in os.listdir("comments"): # Further processing… pass
def disqus(): return DisqusAPI(settings.DISQUS_SECRET_KEY, settings.DISQUS_PUBLIC_KEY)
last_themes_num = 0 last_podcast_num = 0 last_response = None user_themes_posts = None user_themes_timestamp = 0 last_logs = ArrayHandler(max_count=200) logging.basicConfig(format='%(asctime)-15s %(levelname)-8s %(message)s', level=logging.INFO, datefmt="%Y-%m-%d %H:%M:%S", handlers=[logging.StreamHandler(), last_logs]) log = logging.getLogger() disqus = DisqusAPI(public_key=DISQUS_PUBLIC_KEY) disqus_thread_id = 0 def minutes_word(minutes): word = 'минут' if 11 <= minutes <= 14: pass elif minutes % 10 == 1: word += 'у' elif minutes % 10 in [2, 3, 4]: word += 'ы' return word def hours_word(hours):
def get_apicli(): return DisqusAPI(public_key=conf.API_KEY, secret_key=conf.API_SECRET)
def backup_disqus(short_name, key, secret, outfile, min_comments=5, verbose=False): """ backup disqus threads and comments for a given forum shortname :param short_name: Disqus forum short name / ID :type short_name: string :param key: Disqus API public key :type key: string :param secret: Disqus API secret key :type secret: string :param outfile: path to the file to write JSON output to :type outfile: string :param min_comments: minimum number of posts to have, else error and exit :type min_comments: integer (default 5) :param verbose: whether to write verbose output :type verbose: boolean """ result = {} disqus = DisqusAPI(secret, key) if verbose: print("Connected to Disqus API") try: details = disqus.forums.details(forum=short_name) except disqusapi.APIError: sys.stderr.write("ERROR: unable to find forum '%s'\n" % short_name) sys.exit(1) result['forum_details'] = details if verbose: print("Got forum details for '%s': %s" % (short_name, str(details))) try: threads = Paginator(disqus.forums.listThreads, forum=short_name) except APIError: sys.stderr.write("ERROR listing threads for forum '%s'\n" % short_name) sys.exit(1) thread_count = 0 all_threads = [] for t in threads: thread_count = thread_count + 1 all_threads.append(t) if verbose: print("Found %d threads" % thread_count) result['threads'] = all_threads try: posts = Paginator(disqus.forums.listPosts, forum=short_name, include=['unapproved', 'approved']) except APIError: sys.stderr.write("ERROR listing posts for forum '%s'\n" % short_name) sys.exit(1) post_count = 0 all_posts = [] for p in posts: post_count = post_count + 1 all_posts.append(p) if verbose: print("Found %d posts" % post_count) result['posts'] = all_posts j = anyjson.serialize(result) with open(outfile, 'w') as fh: fh.write(j) sys.stderr.write("Output written to %s\n" % outfile) return True
import functools import simplejson import urllib import urllib2 from disqusapi import InvalidAccessToken from disqusapi import DisqusAPI ## TODO: make this read from a file public_key = "PUT YOUR KEY HERE" secret_key = "PUT YOUR KEY HERE" app.secret_key = "this is my salt" disqus = DisqusAPI(secret_key, public_key) ################################################################################ ## UTILS class User(object): """User object based on disqus auth token""" def __init__(self, username, user_id, access_token, expires_in, token_type, refresh_token): super(User, self).__init__() self.username = username self.user_id = user_id self.access_token = access_token self.expires_in = expires_in self.token_type = token_type
#!venv/bin/python from pprint import pprint from requests import post from disqusapi import DisqusAPI disqus = DisqusAPI( "tomRM9FzpoPUBVaTpj9EzJBi7EGtRRJHAK9LWO1LKQxphtyHLRF9Ryq7zrnhWGZc", "g0hQCAzqMrVq2M8DFwpQmnviE22ZSYw4AsuQbRkMTniMD3W5lpIzyvqWEbFNRHt2") disqus.threads.list(forum='climbingroutes')
# coding=utf-8 from disqusapi import DisqusAPI import time import sys import linecache from datetime import datetime import config import os most_likes = config.most_likes limit_nr = config.limit_query #lekerdezett posztok száma top_replies_nr = config.most_popular_partners disqus = DisqusAPI(config.secret_key, config.public_key) listPosts_results = [] user = config.user username = "******" + user if not os.path.exists('dump'): os.makedirs('dump') path = "dump/" + user f = open(path, "w") f_stat = open(path + "stats", "w") class CountLikesDislikes: def __init__(self): time.sleep(0.1) self.likes = {} self.dislikes = {}