<commit_before>import Router from 'koa-router'
import steem from 'steem'
import RSS from 'rss'
import xml from 'xml'

const router = new Router({ prefix: '' })

router.get('/:category/:tag', async (ctx, next) => {
    ctx.type = 'text/xml'
    ctx.body = await rssGenerator(ctx.params.category, ctx.params.tag)   
})

const rssGenerator = async (category, tag) => {
    let feedOption = {
        title: 'Steemit RSS',
        feed_url: `https://steemitrss.com/${tag}`,
        site_url: `https://steemit.com/created/${tag}`,
        image_url: 'https://steemit.com/images/steemit-share.png',
        docs: 'https://imkimchi.github.io/steemit-rss'
    } 

    let apiResponse = await getContent(category, tag)
    let feed = new RSS(feedOption)
    let completedFeed = await feedItem(feed, apiResponse)
    return completedFeed.xml()
}

const getContent = async (category, tag) => {
    let query = { 'tag':tag, 'limit':10 }
    
    let menu = {
        'feed': () => steem.api.getDiscussionsByFeed(query),
        'blog': () => steem.api.getDiscussionsByBlog(query),
        'new': () => steem.api.getDiscussionsByCreated(query),
        'hot': () => steem.api.getDiscussionsByHot(query),
        'trend': () => steem.api.getDiscussionsByTrending(query)
    }

    return await menu[category]()
}

const feedItem = async (feed, response) => {
    console.log(response.length)
    for (let post of response) {
        feed.item({
            title: post.title,
            description: post.body,
            url: `https://steemit.com${post.url}`,
            categories: [post.category],
            author: post.author,
            date: post.created,
            custom_elements: [
                { 'votes': post.net_votes },
                { 'payout_value': post.pending_payout_value }
            ]
        })
    }
    return feed
}

export default router<commit_msg>better performance by declaring methodMap<commit_after>import Router from 'koa-router'
import steem from 'steem'
import RSS from 'rss'
import xml from 'xml'

const router = new Router({ prefix: '' })

router.get('/:category/:tag', async (ctx, next) => {
    ctx.type = 'text/xml'
    ctx.body = await rssGenerator(ctx.params.category, ctx.params.tag)   
})

const rssGenerator = async (category, tag) => {
    let feedOption = {
        title: 'Steemit RSS',
        feed_url: `https://steemitrss.com/${tag}`,
        site_url: `https://steemit.com/created/${tag}`,
        image_url: 'https://steemit.com/images/steemit-share.png',
        docs: 'https://imkimchi.github.io/steemit-rss'
    } 

    let apiResponse = await getContent(category, tag)
    let feed = new RSS(feedOption)
    let completedFeed = await feedItem(feed, apiResponse)
    return completedFeed.xml()
}

const methodMap = {
    'feed': () => steem.api.getDiscussionsByFeed(query),
    'blog': () => steem.api.getDiscussionsByBlog(query),
    'new': () => steem.api.getDiscussionsByCreated(query),
    'hot': () => steem.api.getDiscussionsByHot(query),
    'trend': () => steem.api.getDiscussionsByTrending(query)
}

const getContent = async (category, tag) => {
    let query = { 'tag':tag, 'limit':10 }
    
    if(methodMap.hashasOwnProperty(category)) {
        return await menu[category]()
    } else {
        return Promise.reject(new Error("Unknown Category"));
    }
}

const feedItem = async (feed, response) => {
    console.log(response.length)
    for (let post of response) {
        feed.item({
            title: post.title,
            description: post.body,
            url: `https://steemit.com${post.url}`,
            categories: [post.category],
            author: post.author,
            date: post.created,
            custom_elements: [
                { 'votes': post.net_votes },
                { 'payout_value': post.pending_payout_value }
            ]
        })
    }
    return feed
}

export default router

{'fraction_non_alphanumeric': 0.14667351656819932, 'fraction_numerical': 0.0010274852298998202, 'mean_word_length': 2.585635359116022, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1631226', 'n_tokens_mistral': 1247, 'n_tokens_neox': 1228, 'n_words': 255}