def add_news(request): website_urls=['http://rss.cnn.com/rss/edition.rss','https://news.ycombinator.com/rss ','http://economictimes.indiatimes.com/rssfeedsdefault.cms','http://yourstory.com/feed/',] #request_recvd = request.POST.get('data') for website_url in website_urls: parsed_uri = urlparse( website_url ) domain = '{uri.scheme}://{uri.netloc}/'.format(uri=parsed_uri) print website_url feeds=feedparser.parse(website_url) print feeds for loop in feeds.entries : print loop.published print loop.summary_detail.value print domain print website_url print loop.title news = News(time_stamp = json.dumps(list(loop.published_parsed)),website = domain, url = website_url,title = loop.title,content = loop.summary_detail.value) news.save() # if len([News.objects.get(link = link)]) == 0 : return redirect('/api/')
def test_update_news(mock_model, client, admin): # Given news = News(name='News', text='coucou') news.create() update_obj = { "_id": "news", "name": "Super News", "text": "<b>coucou</b> <script>lol()</script>" } # When res = client.put(url_for('api.update_news', news_id='news'), data=json.dumps(update_obj), content_type='application/json') data = res.json assert res.status_code == 200 assert data['data']['name'] == 'Super News' assert data['data']['_id'] == 'news' assert data['data'][ 'text'] == '<b>coucou</b> <script>lol()</script>' assert dateutil.parser.parse( data['data']['created_at']) < dateutil.parser.parse( data['data']['updated_at'])
def test_pairs(self): saver = HackerNewsSaver() flag = {'value': False} news_items = { 'title0': News(title='title0', url='url0'), 'title1': News(title='title1', url='url1'), } for item in news_items.values(): item.save() item.created = timezone.now() - datetime.timedelta(days=30) item.save() setattr(saver, '_get_content', lambda *args: self._get_content(flag, """\ <table class="itemlist"> <tr class="athing"> <td class="title"> <a href="url1" class="storylink"> title1 </a> </td> </tr> <tr class="athing"> <td class="title"> <a href="url2" class="storylink"> title2 </a> </td> </tr> </table>""")) now = timezone.now() saver.save() self.assertTrue(flag['value'], 'Function _get_content was not called') now_exists = News.objects.all() self.assertLessEqual(len(now_exists), 3, 'Redundant news items were added') self.assertEqual(3, len(now_exists), 'News item was not added') for item in now_exists: if item.title in news_items: self.assertEqual(item.created, news_items[item.title].created, 'created for existed row was updated') else: self.assertGreaterEqual(item.created, now, 'New item has invalid created time') self.assertEqual(item.title, 'title2', 'New item has invalid title') self.assertEqual(item.url, 'url2', 'New item has invalid url') setattr(saver, '_get_content', lambda *args: self._get_content(flag, '')) self.assertRaises(RuntimeError, saver.save)
def setUp(self): now = timezone.make_aware(timezone.datetime(2019, 1, 1, 0, 0, 0), timezone.get_default_timezone()) self._posts = [] for i in range(10): now += timezone.timedelta(minutes=i) post = { 'url': f'url{i}', 'title': f'title{i}', 'created': now.isoformat() } self._posts.append(post) n = News(**post) n.save() post['id'] = n.id n.created = now n.save()
def news(): return News(name='Super News', text='<p>Youpi !</p>')
def seed_data(): """ seed isl teams data from file :return: """ with open(file_name, encoding='utf-8') as fp: contents_of_file = json.load(fp) for data in contents_of_file: news_date = data['news_date'] content = data['content'] source = data['source'] title = data['title'] news_tags = data['news_tags'] sport = data['sport'] is_trending = data['is_trending'] trend_scale = data['trend_scale'] display_order = data['display_order'] image = data['image'] identifier = data['identifier'] try: news = News.objects.get(identifier=identifier) except News.DoesNotExist: news = News() news.news_date = datetime.datetime.strptime(news_date, "%Y-%m-%d").date() news.content = content news.source = source news.title = title news.news_tags = news_tags news.sport = sport news.is_trending = is_trending news.trend_scale = trend_scale news.display_order = display_order news.identifier = identifier news.save() print('added news')
lowPrice=0, closePrice=0) db.session.add(s1) db.session.add(s2) db.session.add(s3) db.session.add(s4) db.session.add(s5) db.session.commit() for stock in ['BAJAJ', 'HDFC', 'ICICI', 'TCS', 'INFY']: r = requests.get( f"https://newsapi.org/v2/everything?q={stock}&apiKey=b49814b3a46e428d9b99b40c568ef6fc" ) json_obj = r.json() for news in json_obj['articles']: save_news = News(name=stock, newsURL=news['url'], text=news['description'], title=news['title']) db.session.add(save_news) db.session.commit() f1 = Favourites(user_id=1, stock_name="BAJAJ") f2 = Favourites(user_id=1, stock_name="TCS") f3 = Favourites(user_id=2, stock_name="HDFC") db.session.add(f1) db.session.add(f2) db.session.commit()
else: cover = headline["cover"]["thumbnailUrl"] date = datetime.datetime.fromtimestamp(headline["date"]) author = headline["author"]["name"] intro = headline["intro"] htmlText = html.fromstring(headline['entryContent']['html']) paragraphs = htmlText.xpath('//p/text()') text = "" for par in paragraphs: text = text + par + "\n\n" print(text) n1 = positiver.evalute(Api.cleanText(text)) n2 = checker.evalute(Api.cleanText(text)) if len(News.objects.all().filter(article_id=article_id)) == 0: article = News(article_id=article_id, title=title, intro=intro, date=date, author=author, cover=cover, article=text, authorPosition=n1, concrete=n2) article.save() time.sleep(10) time.sleep(400)