import csv
import json
import requests
from collections import OrderedDict
from io import StringIO


class UrlData:
    def __init__(self, url):
        self.url = url
        self.r = requests.get(self.url, allow_redirects=True)


class CsvGetter(UrlData):

    def __init__(self, url):
        super().__init__(url)
        self.data = self._parse_csv()

    def _parse_csv(self):
        """
        Get the necessary information from the csv file.
        :return:
        """
        msgs = {}
        f = StringIO(self.r.text)
        reader = csv.reader(f, delimiter=',')
        for row in reader:
            key = row[0]
            msg = row[1]
            msgs[key] = msg
        return msgs


class JsonGetter(UrlData):
    """
        "Answer" : "The current total supply of TRIB as of this moment can be viewed at: https://tribtoken.com/.
        The supply of TRIB is not capped. However, unlike many other tokens, due to TRIB's unique bonding curve
        and internal economics, the price of each TRIB token actually __increases__ as more TRIB are generated by
        people buying new TRIB from the smart contract.",
        "FAQ_Number" : "0002",
        "FAQ_askers" : [],
        "FAQ_authors" : [ "Smathson" ],
        "FAQ_flags" : [ "UNPUBLISHED" ],
        "FAQ_tags" : [ "SUPPLY", "TOKENOMICS" ],
        "Full_Question" : "What is the total supply of TRIB?  Is it capped or will it increase?",
        "Private_Comment" : "",
        "Publish_Status" : "DRAFT",
        "Reference_Name" : "TRIB_supply",
        "Short_Question" : ""
    """

    def __init__(self, url):
        super().__init__(url)
        self.my_json = json.loads(self.r.text)
        self.data = self._create_json_msgs()
        self.entries = self._create_entries_blob()

    def _create_json_msgs(self, short=False):
        """
        Get key/answer pairs from supplied json.  User should be able to supply
        word or number key.
        :return:
        """
        msgs = {}
        for entry in self.my_json:
            short_name = entry['FAQ_Number'].lstrip('0')
            short_name_no_quest = short_name + 's'
            long_name = entry['Reference_Name'].lower()
            private_resp = short_name + 'p'
            long_quest = '**' + entry['Full_Question'] + '**'
            answer = entry['Answer']
            msgs[short_name] = '\n'.join([long_quest, answer])
            msgs[long_name] = '\n'.join([long_quest, answer])
            msgs[short_name_no_quest] = answer
            msgs[private_resp] = '\n'.join([long_quest, answer])
        return msgs

    def _create_entries_blob(self):
        """
        Create an appropriate structure to provide information for the help page.
        :return:
        """
        help_info = OrderedDict()
        for entry in self.my_json:
            short_name = entry['FAQ_Number'].lstrip('0')
            long_name = entry['Reference_Name']
            long_quest = entry['Full_Question']
            short_quest = entry['Short_Question']
            if short_quest:
                question = short_quest
            else:
                question = long_quest
            help_info[short_name] = {'short_name': short_name,
                                     'long_name': long_name,
                                     'question': question,
                                     'full_question': long_quest,
                                     'short_question': short_quest}
        return help_info

import csv
import json
import requests
from collections import OrderedDict
from io import StringIO


class UrlData:
    def __init__(self, url):
        self.url = url
        self.r = requests.get(self.url, allow_redirects=True)


class CsvGetter(UrlData):

    def __init__(self, url):
        super().__init__(url)
        self.data = self._parse_csv()

    def _parse_csv(self):
        """
        Get the necessary information from the csv file.
        :return:
        """
        msgs = {}
        f = StringIO(self.r.text)
        reader = csv.reader(f, delimiter=',')
        for row in reader:
            key = row[0]
            msg = row[1]
            msgs[key] = msg
        return msgs


class JsonGetter(UrlData):
    """
        "Answer" : "The current total supply of TRIB as of this moment can be viewed at: https://tribtoken.com/.
        The supply of TRIB is not capped. However, unlike many other tokens, due to TRIB's unique bonding curve
        and internal economics, the price of each TRIB token actually __increases__ as more TRIB are generated by
        people buying new TRIB from the smart contract.",
        "FAQ_Number" : "0002",
        "FAQ_askers" : [],
        "FAQ_authors" : [ "Smathson" ],
        "FAQ_flags" : [ "UNPUBLISHED" ],
        "FAQ_tags" : [ "SUPPLY", "TOKENOMICS" ],
        "Full_Question" : "What is the total supply of TRIB?  Is it capped or will it increase?",
        "Private_Comment" : "",
        "Publish_Status" : "DRAFT",
        "Reference_Name" : "TRIB_supply",
        "Short_Question" : ""
    """

    def __init__(self, url):
        super().__init__(url)
        self.my_json = json.loads(self.r.text)
        self.data = self._create_json_msgs()
        self.entries = self._create_entries_blob()

    def _create_json_msgs(self, short=False):
        """
        Get key/answer pairs from supplied json.  User should be able to supply
        word or number key.
        :return:
        """
        msgs = {}
        for entry in self.my_json:
            short_name = entry['FAQ_Number'].lstrip('0')
            short_name_no_quest = short_name + 's'
            long_name = entry['Reference_Name'].lower()
            private_resp = short_name + 'p'
            long_quest = '**' + entry['Full_Question'] + '**'
            answer = entry['Answer']
            msgs[short_name] = '\n'.join([long_quest, answer])
            msgs[long_name] = '\n'.join([long_quest, answer])
            msgs[short_name_no_quest] = answer
            msgs[private_resp] = '\n'.join([long_quest, answer])
        return msgs

    def _create_entries_blob(self):
        """
        Create an appropriate structure to provide information for the help page.
        :return:
        """
        help_info = OrderedDict()
        for entry in self.my_json:
            short_name = entry['FAQ_Number'].lstrip('0')
            long_name = entry['Reference_Name']
            long_quest = entry['Full_Question']
            short_quest = entry['Short_Question']
            if short_quest:
                question = short_quest
            else:
                question = long_quest
            help_info[short_name] = {'short_name': short_name,
                                     'long_name': long_name,
                                     'question': question,
                                     'full_question': long_quest,
                                     'short_question': short_quest}
        return help_info

Get the necessary information from the csv file.         :return: "Answer" : "The current total supply of TRIB as of this moment can be viewed at: https://tribtoken.com/.         The supply of TRIB is not capped. However, unlike many other tokens, due to TRIB's unique bonding curve         and internal economics, the price of each TRIB token actually __increases__ as more TRIB are generated by         people buying new TRIB from the smart contract.",         "FAQ_Number" : "0002",         "FAQ_askers" : [],         "FAQ_authors" : [ "Smathson" ],         "FAQ_flags" : [ "UNPUBLISHED" ],         "FAQ_tags" : [ "SUPPLY", "TOKENOMICS" ],         "Full_Question" : "What is the total supply of TRIB?  Is it capped or will it increase?",         "Private_Comment" : "",         "Publish_Status" : "DRAFT",         "Reference_Name" : "TRIB_supply",         "Short_Question" : "" Get key/answer pairs from supplied json.  User should be able to supply         word or number key.         :return: Create an appropriate structure to provide information for the help page.         :return: