#!/usr/bin/python import sys import socket import DemexpRpc import string import os # for debug #count_max=2 count_max=10000 def check_return_code(c): if c != DemexpRpc.const.rt_ok: print "Error: " + DemexpRpc.const.return_code_t[c] \ + " (" + str(c) + ")" sys.exit (1) # Check arguments if len(sys.argv) != 1: print "Usage: make-directories.py" sys.exit (1) # Connect to the server try: c = DemexpRpc.client.Demexp.V1("tuxinette.linux-france.org",50000) except socket.error: print "Connexion failed to tuxinette.linux-france.org:50000" sys.exit (1) # Login on the server login_ret = c.login(DemexpRpc.const.PROTOCOL_VERSION, "demo", "demo") # Check the protocol version returned by the server to see if it's the # same as the one we implement if login_ret.server_protocol_version != DemexpRpc.const.PROTOCOL_VERSION: print "Server has a different protocol version" sys.exit (1) # Check that login was successful check_return_code(login_ret.login_return_code) cookie = login_ret.login_cookie # Get the maximum tag ID ret = c.max_tag_id(cookie) check_return_code(ret.max_tag_id_rc) # Get a hash of tags tag_labels = {} for i in range (0, min(count_max, ret.max_tag_id + 1)): ret = c.tag_info(cookie, i, 1) check_return_code(ret.tag_info_rc) tag_labels[i] = ret.tag_info[0].a_tag_label print str(i) + ". " + tag_labels[i] try: os.mkdir(tag_labels[i]) except OSError: pass # Get the maximum question ID ret = c.max_question_id(cookie) check_return_code(ret.max_question_id_rc) max_question_id = ret.max_question_id # Get the tags of a question tags_of_question = {} for i in range (0, min(count_max, max_question_id + 1)): ret = c.get_question_tags(cookie, i) tags_of_question[i] = ret print ("Tags question %d: " % i) + repr(tags_of_question[i]) # List the questions and their tags for i in range (0, min(count_max, max_question_id + 1)): ret = c.question_info (cookie, i, 1) check_return_code(ret.question_info_rc) question = ret.question_info[0] #print str(i) + ". " + question.q_desc question_tags = [] for t_id in tags_of_question[i]: question_tags.append(tag_labels[t_id]) #print question_tags path = string.join(question_tags, "/") + "/" + question.q_desc print str(i) + ": " + path try: f = open(path,"w") f.write(str(i) + ": " + question.q_desc + "\n") f.write("Tags:" + repr(question_tags) + "\n") f.write("Responses:\n") for response in question.q_info_responses: f.write(" - " + response.r_info_desc + "\n") f.close() except (OSError, IOError): pass # Disconnect c.goodbye(cookie) sys.exit(0)