def post(self): title = self.request.get('title') body = self.request.get('news_body') tags = self.request.get('tags') author = self.request.get('author') self.response.headers['Content-Type'] = 'application/json' response = {} if (not title) or (not body): response['success'] = 0 response['message'] = 'Field(s) empty' else: n = News(); n.title = title n.body = body n.author = self.session.get('username') n.tags = tags n.rId = randint(0, 999999999)#God doesnt play dice, I do n.put() response['success'] = 1 response['message'] = 'News added successfully' #self.redirect('/') self.response.write(json.dumps(response))
def get(self): self.response.headers['Content-Type'] = 'application/json' response = {} #reponse is json object id = self.request.get('id') try: id = int(id) except: #error response['success'] = 0 response['message'] = 'An error occured' self.response.write(json.dumps(response)) return news = News.get_by_id(id); if not news: #error no news with the id response['success'] = 0 response['message'] = 'No news with the id' self.response.write(json.dumps(response)) return else: response = news_to_dict(news, True) self.response.write(json.dumps(response))
def news_seeder(amount): for _ in range(amount): news_dict = { 'title': lorem.sentence()[:15], 'content': lorem.paragraph()[:255] } News(**news_dict).save()
def query_news(self, diario): try: hoy = date.today() e = News.query( News.date == hoy, News.news_from == diario, News.in_use == False, ).order(News.id) return e except: print traceback.format_exc() return traceback.format_exc()
def _create_news(data_item, state, country): news_id = sha256( (data_item['url'] + state + country).encode('utf-8')).hexdigest() return News(id=news_id, title=data_item['title'], url=data_item['url'], img_url=data_item['urlToImage'], description=data_item['description'], publishedAt=data_item['publishedAt'], source=data_item['source']['name'], content=data_item['content'], state=state, country=country)
def save(self, author, *args, **kwargs): user = get_object_or_404(User, username=author) new = News( news_category=self.validated_data['news_category'], news_head=self.validated_data['news_head'], news_body=self.validated_data['news_body'], ) new.set_news_author(user) img = None if 'str_image' in self.validated_data and self.validated_data[ 'str_image'] is not None: img = base64_file(self.validated_data['str_image']) new.set_news_image(img) new.save() return new
def get(self): self.response.headers['Content-Type'] = 'application/json' response = [] #reponse is json array start = self.request.get('start') idadi = self.request.get('idadi') id = self.request.get('id') start = self.toNumber(start, 0) idadi = self.toNumber(idadi, self.DEFAULT_NEWS_IDADI) + 1 #limit must be > 0 news = News.query().order(-News.date).fetch(offset=start, limit=idadi) for news_entry in news: response.append(news_to_dict(news_entry)) self.response.write(json.dumps(response))
def get(self): hoy = date.today() last = News.query(News.date == hoy, ).order(News.id).get() diarios = [ "montevideo_com", "el_observador", "el_pais", "ciento_ochenta", "uy_press" ] cont = 0 for i in diarios: query = self.query_news(i) for e in diarios: if i != e: query2 = self.query_news(e) cont = self.selector_news(query, query2, i, e, cont) del diarios[0] post = self.post_selector() return webapp2.redirect('/')
def get(self): hoy = date.today() last = News.query(News.date == hoy, ).order(News.id).get() # MONTVIDEO COM mc = News.query( News.date == hoy, News.news_from == "montevideo_com", News.in_use == False, ).order(News.id) ep = News.query( News.date == hoy, News.news_from == "el_pais", News.in_use == False, ).order(News.id) cont = self.selector_news(ep, mc, "el_pais", "montevideo_com") # EL OBSERVADOR print "\n" print "1 vuelta" print "cont" print cont eo2 = News.query( News.date == hoy, News.news_from == "el_observador", News.in_use == False, ).order(News.id) mc2 = News.query( News.date == hoy, News.news_from == "montevideo_com", News.in_use == False, ).order(News.id) cont2 = self.selector_news(eo2, mc2, "el_observador", "montevideo_com", cont) print "\n" print "2 vuelta" print "cont" print cont2 # CIENTO OCHENTA co3 = News.query( News.date == hoy, News.news_from == "ciento_ochenta", News.in_use == False, ).order(News.id) mc3 = News.query( News.date == hoy, News.news_from == "montevideo_com", News.in_use == False, ).order(News.id) cont3 = self.selector_news(co3, mc3, "ciento_ochenta", "montevideo_com", cont2) print "\n" print "3 vuelta" print "cont" print cont3 # UY PRESS co35 = News.query( News.date == hoy, News.news_from == "uy_press", News.in_use == False, ).order(News.id) mc35 = News.query( News.date == hoy, News.news_from == "montevideo_com", News.in_use == False, ).order(News.id) cont35 = self.selector_news(co35, mc35, "uy_press", "montevideo_com", cont3) print "\n" print "3.5 vuelta" print "cont" print cont35 eo4 = News.query( News.date == hoy, News.news_from == "el_observador", News.in_use == False, ).order(News.id) ep4 = News.query( News.date == hoy, News.news_from == "el_pais", News.in_use == False, ).order(News.id) cont4 = self.selector_news(eo4, ep4, "el_observador", "el_pais", cont3) eo45 = News.query( News.date == hoy, News.news_from == "uy_press", News.in_use == False, ).order(News.id) ep45 = News.query( News.date == hoy, News.news_from == "el_pais", News.in_use == False, ).order(News.id) cont45 = self.selector_news(eo4, ep4, "uy_press", "el_pais", cont4) print "\n" print "45 vuelta" print "cont" print cont4 co5 = News.query( News.date == hoy, News.news_from == "ciento_ochenta", News.in_use == False, ).order(News.id) ep5 = News.query( News.date == hoy, News.news_from == "el_pais", News.in_use == False, ).order(News.id) cont5 = self.selector_news(co5, ep5, "ciento_ochenta", "el_pais", cont45) print "\n" print "5 vuelta" print "cont" print cont5 eo6 = News.query( News.date == hoy, News.news_from == "el_observador", News.in_use == False, ).order(News.id) co6 = News.query( News.date == hoy, News.news_from == "ciento_ochenta", News.in_use == False, ).order(News.id) cont6 = self.selector_news(co6, eo6, "ciento_ochenta", "el_observador", cont5) eo7 = News.query( News.date == hoy, News.news_from == "el_observador", News.in_use == False, ).order(News.id) co7 = News.query( News.date == hoy, News.news_from == "uy_press", News.in_use == False, ).order(News.id) cont7 = self.selector_news(co6, eo6, "uy_press", "el_observador", cont6) print "\n" print "7 vuelta" print "cont" print cont7 eo8 = News.query( News.date == hoy, News.news_from == "ciento_ochenta", News.in_use == False, ).order(News.id) co8 = News.query( News.date == hoy, News.news_from == "uy_press", News.in_use == False, ).order(News.id) cont8 = self.selector_news(co6, eo6, "uy_press", "ciento_ochenta", cont7) print "\n" print "8 vuelta" print "cont" print cont8 hoy = date.today() return True query = NewsSelect.query().order(NewsSelect.id) template_values = { "data": query, "fecha": hoy, } template = main.JINJA_ENVIRONMENT.get_template( '/templates/profile.html') self.response.write(template.render(template_values))
def selector_news(self, data, data2, news_from, news_from2, cont=0): try: hoy = date.today() last = News.query(News.date == hoy, ).order(News.id).get() list_keyword_data = [] for i in data: keyword = i.keyword title = i.title keys = (i.key).urlsafe() d = { "keyword": i.keyword, "title": i.title, "subtitle": i.subtitle, "url": i.url, "image": i.image, "category": i.category, "key": i.key, } list_keyword_data.append(d) print "entrando" if int(cont) > 0: cont = int(cont) else: cont = 0 for i in data2: keyword_data2 = i.keyword for e in list_keyword_data: key = e["keyword"] if key and keyword_data2: a = set(keyword_data2) b = set(key) if a.intersection(b): c = a.intersection(b) if len(c) > 2: print "similar" similar_data = { "title": e["title"], "subtitle": e["subtitle"], "image": e["image"], "url": e["url"], "category": e["category"], "news_from": news_from, } exist = NewsSelect.query( NewsSelect.title == i.title, NewsSelect.news_from == news_from2, NewsSelect.date == hoy, NewsSelect.is_similar == True, ).get() if not exist: cont += 1 save_me = NewsSelect( title=i.title, subtitle=i.subtitle, image=i.image, url=i.url, category=i.category, news_from=news_from2, html=i.html, similar=[similar_data], cant_similar=1, keyword_set=[c], keyword=[keyword_data2, key], is_similar=True, id=cont, hour=last.hour, ) save_me.put() print "post save" in_use = News.query( News.key == e["key"]).get() in_use.in_use = True in_use.put() else: cont += 1 exist.similar.append(similar_data) exist.cant_similar = exist.cant_similar + 1 exist.keyword_set.append(c) exist.keyword.append(key) exist.id = cont exist.hour = last.hour exist.put() in_use = News.query( News.key == e["key"]).get() in_use.in_use = True in_use.put() similar_data = {} return cont except: print traceback.format_exc() return traceback.format_exc()
def delete(self, n_id): obj = News.objects(id=n_id).get() obj.delete() return {n_id: 'DELETED'}
def put(self, n_id): obj = News.objects(id=n_id).get() obj.update(**request.json) return NewsScheme().dump(obj.reload())
def post(self): obj = News(**request.json).save() return NewsScheme().dump(obj)
def get(self, n_id=None): if n_id: return NewsScheme().dump(News.objects(id=n_id).get()) return NewsScheme().dump(News.objects, many=True)
def post_selector(self): try: hoy = date.today() e = NewsSelect.query( NewsSelect.date == hoy, NewsSelect.is_similar == True, ) for i in e: title = i.title for o in e: if title != o.title: keyword_set_a = i.keyword keyword_set_b = o.keyword a = set(keyword_set_a[0]) a2 = set(keyword_set_a[1]) b = set(keyword_set_b[0]) b2 = set(keyword_set_b[1]) if (a.intersection(b) or a.intersection(b2) or a2.intersection(b) or a2.intersection(b2)): c = a.intersection(b) c2 = a.intersection(b2) c3 = a2.intersection(b) c4 = a2.intersection(b2) if (len(c) > 2 or len(c2) > 2 or len(c3) > 2 or len(c4) > 2): try: in_use = NewsSelect.query( NewsSelect.title == i.title, ).get() similar = { "keyword": in_use.keyword, "title": in_use.title, "subtitle": in_use.subtitle, "url": in_use.url, "image": in_use.image, "category": in_use.category, } except: print "\n" print[i.title] print traceback.format_exc() continue try: in_useb = NewsSelect.query( NewsSelect.title == o.title, ).get() similarb = { "keyword": in_useb.keyword, "title": in_useb.title, "subtitle": in_useb.subtitle, "url": in_useb.url, "image": in_useb.image, "category": in_useb.category, } except: print "\n" print[o.title] print traceback.format_exc() continue if i.cant_similar >= o.cant_similar: in_use.similar.append(in_useb.similar[0]) in_use.similar.append(similarb) in_use.cant_similar = in_use.cant_similar + 1 in_use.keyword_set.append(keyword_set_b) #in_use.keyword.append(i.keyword) in_use.put() in_useb.key.delete() print "\n" print in_use print "\n" use = News.query( News.title == i.title).get() use.in_use = True use.put() else: in_useb.similar.append(in_use.similar[0]) in_use.similar.append(similar) in_useb.cant_similar = in_useb.cant_similar + 1 in_useb.keyword_set.append(keyword_set_a) #in_useb.keyword.append(o.keyword) in_useb.put() in_use.key.delete() print "\n" print in_useb print "\n" use = News.query( News.title == o.title).get() use.in_use = True use.put() return webapp2.redirect('/') except: print traceback.format_exc() return traceback.format_exc()