#!/usr/bin/env python 
#-*- coding: utf-8 -*- 

import sys 
reload(sys) 
sys.setdefaultencoding('UTF-8')
sys.path.append('../..')

from config.settings import Collection, db
import feedparser
FILE_PREFIX = '/home/work/newsman/newsman/bin/text_based_feeds/feed_lists/'
#FILE_PREFIX = '/home/ubuntu/newsman/newsman/bin/text_based_feeds/feed_lists/'
#FILE_PREFIX = '/home/jinyuan/Downloads/newsman/newsman/bin/text_based_feeds/feed_lists/'


def _parse_task(line):
    """
    read *_feeds_list.txt
    """
    line = line.strip()
    if line:
        task = line.strip().split('*|*')
        # task[1] refers to categories
        return task[0].strip(), task[1].strip(), task[2].strip(), task[3].strip()
    else:
        return None

def _find(language, country):
    """
    """
    feeds_list = open('%s%s_%s_feeds_list' % (FILE_PREFIX, language, country), 'r')
    lines = feeds_list.readlines()
    feeds_list.close()
    
    for line in lines:
        language, category, transcoder, link = _parse_task(line)
        ss = feedparser.parse(link)
        feed_title = ss['feed']['title'] if ss and 'title' in ss['feed'] else None
        print "[%s]" % str(feed_title), link

    """
    col = Collection(db, 'feeds')

    output = {}
    items = col.find()
    for item in items:
        for country in item['countries']:
            country = str(country)
            if country not in output:
                output[country] = []
            ss = feedparser.parse(item['feed_link'])
            print item['feed_link']
            feed_title = ss['feed']['title'] if ss and 'title' in ss['feed'] else item['feed_link']
            print str(feed_title)
            output[country].append('%s  %s' % (item['categories'][0], str(feed_title)))
            print

    f = open('test', 'w')
    for k, v in output.iteritems():
        f.write(k + '\n')
        for i in v:
            f.write("    " + i + '\n')
        f.write('\n')
    f.close()
    """

if __name__ == "__main__":
    if len(sys.argv) > 1:
        _find(sys.argv[1], sys.argv[2])
    else:
        print 'Please indicate a language and country'

@@ -920,16 +920,102 @@
 ()%0A    %0A
+    feed_titles = open('%25s%25s_%25s_feed_titles' %25 (FILE_PREFIX, language, country), 'w')%0A
     for 
@@ -1257,16 +1257,103 @@
 e), link
+%0A        feed_titles.write(%22%25s*%7C*%25s%22 %25 (link, str(feed_title)))%0A    feed_titles.close()
 %0A%0A    %22%22

Add output code segment