def news_release(): #进行页面渲染 if request.method == "GET": try: categories = Category.query.all() except Exception as e: current_app.logger.error(e) return jsonify(errno=Code.DBERR, errmsg="获取分类失败") category_list = [] for category in categories: category_list.append(category.to_dict()) return render_template("news/user_news_release.html", categories=category_list) # 数据提交 title = request.form.get("title") category_id = request.form.get("category_id") digest = request.form.get("digest") index_image = request.files.get("index_image") content = request.form.get("content") if not all([title, category_id, digest, index_image, content]): return jsonify(errno=Code.PARAMERR, errmsg="参数不全") try: #读取图片为二进制数据,上传 image_name = image_storage(index_image.read()) except Exception as e: current_app.logger.error(e) return jsonify(errno=Code.THIRDERR, errmsg="七牛云异常") if not image_name: return jsonify(errno=Code.NODATA, errmsg="图片上传失败") news = News() news.title = title news.source = g.user.nick_name news.digest = digest news.content = content news.index_image_url = constants.QINIU_DOMIN_PREFIX + image_name news.category_id = category_id news.user_id = g.user.id news.status = 1 #表示审核中 try: db.session.add(news) db.session.commit() except Exception as e: current_app.logger.error(e) return jsonify(errno=Code.DBERR, errmsg="新闻发布失败") # print("发布成功") return jsonify(errno=Code.OK, errmsg="发布成功")
def parser3(): url = 'https://www.gazeta.ru/export/rss/first.xml' r = requests.get(url) soup = BeautifulSoup(r.text, 'html.parser') item_xml = soup.find_all('item')[0] get_title = item_xml.find('title').text get_category = item_xml.find('category').text get_link = item_xml.find('guid').text icon = 'https://www.gazeta.ru/i/gazeta_og_image.jpg' list_category = [] list_title = [] for c in Category.objects.all(): list_category.append(c.name) if get_category in list_category: print(get_category, " Категория существует") elif get_category not in list_category: new_category = Category() new_category.name = get_category new_category.save() print("Добавлена новая категория ", get_category) for t in News.objects.all(): list_title.append(t.title) if get_title in list_title: print("новость существует в базе") elif get_title not in list_title: print("Добавление новой новости") new_news = News() new_news.title = get_title new_news.category_id = Category.objects.get(name=get_category).pk new_news.url_source = get_link new_news.icon = icon new_news.save()
def add_to_base(img_file, news_parser_rss): logging.basicConfig(filename='add_to_base.log', filemode='a', format='%(filename)s[LINE:%(lineno)d]# ' '%(levelname)-8s [%(asctime)s] %(message)s', level=logging.WARNING) try: news_list = [] russia = ["россия", "вроссии"] economic = ["экономика", "финансы", "бизнес"] turism = ["путешествия", "туризм"] sng = ["бывшийссср", "снг"] world = ["мир", "вмире"] scince = ["наукаитехника", "digital", "наука", "технологии"] politics = ["власть", "политика"] culture = ["кинократия", "культура"] p = news_parser_rss title = p['title'] category = p['category'] description = p['description'] source = p['source'] link_news = p['link'] if p['media'] == None: pass else: media = p['media'] news = News.objects.all() for n in news: news_list.append(n.title.strip().lower().replace(' ', '')) if title.strip().lower().replace(' ', '') not in news_list: new = News() new.title = title if category.lower().replace(' ', '') in russia: new.category_id = Category.objects.get(name='Россия').pk new.url_source = link_news new.description = description new.media = media new.icon = 'news/images/' + img_file new.save() elif category.lower().replace(' ', '') in economic: new.category_id = Category.objects.get(name='Экономика').pk new.url_source = link_news new.media = media new.description = description new.icon = 'news/images/' + img_file new.save() elif category.lower().replace(' ', '') in sng: new.category_id = Category.objects.get(name='СНГ').pk new.url_source = link_news new.media = media new.description = description new.icon = 'news/images/' + img_file new.save() elif category.lower().replace(' ', '') in turism: new.category_id = Category.objects.get(name='Туризм').pk new.url_source = link_news new.media = media new.description = description new.icon = 'news/images/' + img_file new.save() elif category.lower().replace(' ', '') in world: new.category_id = Category.objects.get(name='Мир').pk new.url_source = link_news new.description = description new.media = media new.icon = 'news/images/' + img_file new.save() elif category.lower().replace(' ', '') == "спорт": new.category_id = Category.objects.get(name='Спорт').pk new.url_source = link_news new.media = media new.description = description new.icon = 'news/images/' + img_file new.save() elif category.lower().replace(' ', '') in scince: new.category_id = Category.objects.get( name='Наука и техника').pk new.url_source = link_news new.media = media new.description = description new.icon = 'news/images/' + img_file new.save() elif category.lower().replace(' ', '') == "силовыеструктуры": new.category_id = Category.objects.get( name='Силовые структуры').pk new.url_source = link_news new.media = media new.description = description new.icon = 'news/images/' + img_file new.save() elif category.lower().replace(' ', '') in politics: new.category_id = Category.objects.get(name='Политика').pk new.url_source = link_news new.media = media new.description = description new.icon = 'news/images/' + img_file new.save() elif category.lower().replace(' ', '') in culture: new.category_id = Category.objects.get(name='Культура').pk new.url_source = link_news new.media = media new.description = description new.icon = 'news/images/' + img_file new.save() elif category.lower().replace(' ', '') == "происшествия": new.category_id = Category.objects.get(name='Происшествия').pk new.url_source = link_news new.media = media new.description = description new.icon = 'news/images/' + img_file new.save() elif category.lower().replace(' ', '') == "нацпроекты": new.category_id = Category.objects.get(name='Нацпроекты').pk new.url_source = link_news new.media = media new.description = description new.icon = 'news/images/' + img_file new.save() elif category.lower().replace(' ', '') == "авто": new.category_id = Category.objects.get(name='Авто').pk new.url_source = link_news new.media = media new.description = description new.icon = 'news/images/' + img_file new.save() else: pass except Exception as e: msg = category, e logging.error(msg)