Beispiel #1
0
def post():
    if session['login']:
        form = forms.PostForm()
        if form.validate_on_submit():
            db.add_post(user_id=str(session['user_id']),
                        article=form.content.data.strip())
            flash('Your Message has been posted!', 'success')
            return redirect(url_for('index'))
        return render_template('post.html', form=form)
Beispiel #2
0
    def test_adding_post(self):
        post = {
            'post_id': 1,
            'title': 'test',
            'url': 'test',
        }

        add_post(**post)

        self.assertEqual(mocked_posts.estimated_document_count(), 1)
def vk_post_parsing(link):  #парсит пост в группе
    r = requests.get("https://m.vk.com" + str(link))
    soup = BeautifulSoup(str(r.text), "html.parser")
    post = soup.findAll("div", class_="wall_item")
    try:
        author = soup.find("a", class_="pi_author")['href']
    except:
        db.delete_db_post(link)
    inpost = BeautifulSoup(str(post), "html.parser")
    if soup.title.text == "Ошибка" or soup.title.text == "Error":
        print(link + " Error")
        db.delete_db_post(link)
        return None
    try:
        text = inpost.find("div", class_="pi_text").text
    except:
        text = ""
    try:
        likes = inpost.find("b", class_="v_like").string.replace("K", "000")
    except:
        likes = "0"
    try:
        shares = inpost.find("b", class_="v_share").string.replace("K", "000")
    except:
        shares = "0"
    try:
        views = inpost.find("b", class_="v_views").string.replace(
            "K", "000").replace(".", "")
    except:
        views = "0"
    images = ""
    for img in inpost.findAll("div",
                              class_="thumb_map_img thumb_map_img_as_div"):
        try:
            images = images + img['data-src_big'].split("|")[0] + " ,\n"
        except:
            images = images
    post = {
        "id": link,
        "author": author,
        "text": text,
        "images": images,
        "likes": likes,
        "shares": shares,
        "views": views
    }
    repost = inpost.find("div", class_="pic_header")  #если репост
    if repost:  #если репост отправляем паблик в очередь
        repost_link = BeautifulSoup(str(repost),
                                    "html.parser").find("a")['href']
        db.add_to_queue(post, repost_link)
        return False
    db.add_post(link, post)
Beispiel #4
0
def add_posts_into_db() -> int:
    added_posts_count = 0
    for post in get_posts():
        db_post = get_post_by_external_id(post['post_id'])
        if db_post is not None:
            continue
        add_post(
            post_id=post['post_id'],
            title=post['title'],
            url=post['url'],
        )
        added_posts_count += 1
    return added_posts_count
Beispiel #5
0
 def _add_post(self, insert_data):
   """Inserts the given PostInsertData into the database."""
   self.assertIsNotNone(insert_data)
   post_id = db.add_post(
       insert_data.user_id, insert_data.data, insert_data.hash_tags, now=insert_data.now)
   self.assertIsNotNone(post_id)
   return post_id
Beispiel #6
0
Datei: index.py Projekt: ab/blag
 def add_post(self, body=None, title=None, submit=None):
     author = get_author()
     if author is None:
         raise cherrypy.HTTPRedirect("/login")
     if cherrypy.request.method.lower() == "get":
         return {"page_title":"Create Post"}
     post_id = int(db.add_post(author["id"], title, body))
     raise cherrypy.HTTPRedirect("/entry/%d" % post_id)
Beispiel #7
0
def _add_post(user_id, text):
  """Adds a post to the database derived from the given text, by the given user.

  This method returns the identifier of the added post.
  """
  processed_post = post_processor.process(text)
  data_string = json.dumps(processed_post.data)
  post_id = db.add_post(user_id, data_string, processed_post.hash_tags)
  return post_id
Beispiel #8
0
def add_post(body):
    """Add a new post
    :param body: Post details to be added
    :type body: dict | bytes

    :rtype: None
    """
    if connexion.request.is_json:
        body = connexion.request.get_json()
    return {"id": db.add_post(body)}
def newpost(request):
    post = request.form.get('submission-text')
    if (len(post) > 280):
        return redirect('/')

    preview = None
    link = None

    for word in post.split(' '):
        if word.startswith('[link]'):
            link = " ".join(word.split('[link]')[1:]).strip()
            if verified_user(session, request.session.get('username'))[0]:
                preview = get_post_preview(link)
            link = link
            break

    post = post.replace('[link]', '')

    add_post(session, request.session.get('username'), post, link, preview)

    return redirect('/')
Beispiel #10
0
def new_post():
    data = json.loads(request.get_data('data'))
    title = data.get('title')
    content = data.get('content')
    bar_path = data.get('bar_path')
    imgs_list = data.get('imgs')
    imgs = []
    for i in imgs_list:
        img_base64 = (i['base64'])[23:]
        try:
            img_data = base64.b64decode(img_base64)
        except:
            pass
        img_path = str(uuid.uuid1())
        post_img = 'posts_imgs/' + img_path + '.jpg'
        imgs.append(post_img)
        with open('static/' + post_img, 'wb+') as f:
            f.write(img_data)
    bar = db.db_session.query(db.Bar).filter(db.Bar.b_path == bar_path).first()
    current_user = db.db_session.query(
        db.User).filter(db.User.u_name == session.get('current_user')).first()
    db.add_post(title, content, bar.b_id, current_user.u_id, imgs)
    return jsonify({'status': 0})
Beispiel #11
0
    def POST(self, tid):
        tid = utils.dec_tid(tid)
        data = web.input()
        if 'back' in data:
            raise web.seeother('/forum#top')

        elif 'help' in data:
            raise web.seeother('http://www.markitdown.net/markdown#top')
            
        elif 'post' in data:
            if session.page_data['preview']:
                message = session.page_data['message']
                
            else:
                message = data.message
                
            if message:
                if db.add_post(session.uid, tid, message):
                    db.mark_read(session.uid, tid)
                    session.page_data['preview'] = False
                    session.page_data['message'] = ''
            
        elif 'preview' in data:
            if data.message:
                session.page_data['preview'] = True
                session.page_data['message'] = data.message

        elif 'edit' in data:
            session.page_data['preview'] = False

        elif 'nbsp.x' in data:
            session.page_data['message'] = data.message + ' '

        else: #emots
            # in the list of keys we shoud find emot_{name}.png.x and
            # emot_name.png.y where {name} is variable
            keys = '\n'.join(data.keys())
            emot = re.search('^emot_(.*)\.x', keys, re.M).groups()[0]
            
            # append markdown tag at the end of message
            session.page_data['message'] = data.message + '![](/static/em/%s)' % emot

        raise web.seeother(web.ctx.path + '#bottom') # stay here, only refresh
Beispiel #12
0
def save_post(driver_json, user_id, link, likes, location):
    try:
        driver_json.get(f"{link}{config.DATA_TO_JSON}")
        data = json.loads(
            BeautifulSoup(driver_json.page_source,
                          'html.parser').find('body').get_text())
        full_post = data["graphql"]["shortcode_media"]
        post = {
            "user_id":
            user_id,
            "link":
            link,
            "caption":
            full_post["edge_media_to_caption"]["edges"][-1]["node"]["text"]
            [:255],
            "likes":
            full_post["edge_media_preview_like"]["count"],
            "comments":
            full_post["edge_media_to_parent_comment"]["count"],
            "is_video":
            full_post["is_video"],
            "views":
            full_post["video_view_count"]
            if "video_view_count" in full_post else None,
            "temperature":
            None,
            "weather":
            None,
            "timestamp":
            full_post["taken_at_timestamp"]
        }

        # if there is a location let's add it
        full_location = full_post["location"]
        location_id = None
        if full_location:
            address = json.loads(full_location["address_json"]
                                 ) if full_location["address_json"] else None
            location = {
                "name":
                full_location["name"],
                "slug":
                full_location["slug"],
                "country":
                address["country_code"] if address else None,
                "city":
                address["city_name"]
                if address and len(address["city_name"]) > 0 else None
            }
            # Getting Geo location from google API
            geo_address = f"{location['city']}, {location['country']}"
            lat, lon = weather_api.google_geo(geo_address)
            location["latitude"] = lat
            location["longitude"] = lon
            # Getting weather from open weather API
            temperature, weather = weather_api.get_weather(
                lat, lon, post["timestamp"])
            post["temperature"] = temperature
            post["weather"] = weather

            location_id = db.add_location(location)

        # We save the post
        post_id = db.add_post(user_id, post)

        # If there is a location we save the aux table entry
        if location_id:
            db.add_post_location(post_id, location_id)

        return post_id
    except json.decoder.JSONDecodeError as ex:
        print(ex)
        post_id = db.add_simple_post(user_id, link, likes)
        if location:
            location_id = db.add_simple_location(location)
            db.add_post_location(post_id, location_id)
        return post_id
Beispiel #13
0
def post_story():
    content = request.form.get('content')
    add_post(content)
    return render_template('layout.html', data=get_allposts())
Beispiel #14
0
Datei: reset.py Projekt: ab/blag
def hard_reset():
    db.drop_tables(verbose=True)
    db.create_dbs()
    db.add_author('dude', 'dude')
    db.add_author('guy', 'guy')
    db.add_post(1, 'Hello!', 'The quick brown fox jumps over the lazy dog.')