Пример #1
1
def dashboard(request):
    if request.user.is_authenticated():
        r = requests.get(
            'https://newsapi.org/v1/articles?source=mirror&apiKey=6df0769e0d6244aaa00768c02f123fb2'
        )
        response_dictionary = {}
        response_dictionary["source"] = "mirror"
        data = json.loads(r.text)
        list_of_urls = []
        article_data = []
        main_dict = {}
        i = 0
        for item in data["articles"]:
            mid_dictionary = {}
            if (type(item["title"]) == "unicode"):
                ti = unicodedata.normalize('NFKD', item["title"]).encode(
                    'ascii', 'ignore')
            else:
                ti = item["title"]
            n = len(ti)
            if (n > 10):
                mid_dictionary["title"] = ti
                sc = Scraper()
                print item
                mid_dictionary["data"] = sc.scrape_mirror(item["url"])
                article_data.append(mid_dictionary)
                mid_dictionary["image"] = (item["urlToImage"])
                print mid_dictionary["image"]
                if (type(mid_dictionary["data"]) == "unicode"):
                    st = unicodedata.normalize('NFKD',
                                               mid_dictionary["data"]).encode(
                                                   'ascii', 'ignore')
                else:
                    st = mid_dictionary["data"]

                try:

                    print "********Summary******"
                    summary = summarizer.summarize(st, words=50)
                    print summary.encode('ascii', 'ignore')
                    print "---------Summary---------"
                    mid_dictionary["summary"] = summarizer.summarize(st,
                                                                     words=50)
                except ZeroDivisionError:
                    mid_dictionary["summary"] = st

                print mid_dictionary
                main_dict["article_" + str(i)] = mid_dictionary
                i = i + 1

        response_dictionary["articles"] = article_data

        print main_dict
        for key, value in main_dict.iteritems():
            print "***************"

        return render(request, 'accounts/dashboard.html',
                      {'main_dict': main_dict})
    else:
        return HttpResponseRedirect("/login")
Пример #2
0
def dashboard(request):
    if request.user.is_authenticated():
        r = requests.get('https://newsapi.org/v1/articles?source=mirror&apiKey=6df0769e0d6244aaa00768c02f123fb2')
        response_dictionary = {}
        # response_dictionary["status"] = r
        response_dictionary["source"] = "mirror"
        data = json.loads(r.text)
        # print data["articles"]
        list_of_urls = []
        article_data = []
        main_dict = {}
        i = 0;
        for item in data["articles"]:
            mid_dictionary = {}
            mid_dictionary["title"] = item["title"]
            #mid_dictionary["url"] = item["url"]
            #mid_dictionary["urlToImage"] = item["urlToImage"]
            sc = Scraper()
            mid_dictionary["data"] = sc.scrape_mirror(item["url"])
            article_data.append(mid_dictionary)
            print type(mid_dictionary["data"])
            if(type(mid_dictionary["data"])=="unicode"):
                st = unicodedata.normalize('NFKD', mid_dictionary["data"]).encode('ascii','ignore')
            else:
                st = mid_dictionary["data"]
            if(len(st)>250):
                print "********Summary******"
                print summarizer.summarize(st,words=50)
                print "---------Summary---------"
                mid_dictionary["summary"] = summarizer.summarize(st,words=50)
            else :
                mid_dictionary["summary"] = st

            mid_dictionary["article_"+str(i)] = mid_dictionary
            i = i+1
        
        response_dictionary["articles"] = article_data
        
        # news_dictionary = {}
        #print main_dict
        for key,value in main_dict.iteritems():
            print "***************"
            # print key
            
            
            print value["title"]
        # # print news_dictionary
        # list_of_urls = []
        # return render(request, 'newslist.html', {'newslist':response_dictionary})
        return render(request,
                'accounts/dashboard.html',
                {'main_dict': main_dict}
                )
    else:
         return HttpResponseRedirect("/login")