Exemplo n.º 1
0
def handleForumPost(postId):
    try:
        post = a.ForumPost.q.filter(a.ForumPost.id == postId).first()

        post_log.set_description_str(f'🧾  Post: {post.title[:40]}...')
        if post != None:
            wpost = w.ForoPost.q.filter(w.ForoPost.postid == post.id).first()
            if wpost == None:
                wpost = w.ForoPost()

            parentid = post.parentPost_id
            if parentid == None:
                parentid = 0

            wp_user = handleUser(post.createdBy_id)

            wpost.postid = post.id
            wpost.forumid = post.category_id
            wpost.topicid = post.thread_id
            wpost.user = wp_user
            wpost.title = post.title
            wpost.body = post.contentAsHtml
            wpost.created = post.creationDate
            wpost.modified = post.modificationDate
            wpost.parentid = parentid
            wpost.status = "0"

            w.session.add(wpost)
            w.session.commit()

            for rating in post.ratings:
                logging.info(f'handle rating {rating}')
                wp_rating_user = handleUser(rating.ratingUser_id)
                wp_foro_like = w.ForoLike.q.filter(
                    w.ForoLike.userid == wp_rating_user.ID,
                    w.ForoLike.postid == wpost.postid,
                    w.ForoLike.post_userid == wpost.userid).first()
                if wp_foro_like == None:
                    wp_foro_like = w.ForoLike()

                wp_foro_like.userid = wp_rating_user.ID
                wp_foro_like.postid = wpost.postid
                wp_foro_like.post_userid = wpost.userid

                w.session.add(wp_foro_like)
            w.session.commit()

            return wpost
        else:
            return None
    except Exception as e:
        logging.error(e)
Exemplo n.º 2
0
def handleArticleComment(commentId):
    try:
        comment = a.ArticleComment.q.filter(
            a.ArticleComment.id == commentId).first()

        if comment == None:
            logging.error(f'comment with id: {commentId} not found')
            return

        wp_user = handleUser(comment.createdBy.id)
        wp_post = w.Post.q.join(w.PostMeta).filter(
            w.PostMeta.meta_key == 'legacy_article_id',
            w.PostMeta.meta_value == f'{comment.article.id}').first()

        if wp_post is None:
            logging.error(
                f'skipping comment, since article: {comment.article.id} not present in wordpress'
            )
            return

        parent = None
        if comment.parent:
            parent = handleArticleComment(comment.parent.id)

        wp_comment = w.Comment.q.join(w.CommentMeta).filter(
            w.CommentMeta.meta_key == 'legacy_comment_id',
            w.CommentMeta.meta_value == f'{comment.id}').first()

        if wp_comment == None:
            wp_comment = w.Comment()
            wp_comment.user = wp_user
            wp_comment.post = wp_post

        wp_comment.comment_content = comment.comment
        wp_comment.comment_date = comment.modificationDate
        wp_comment.comment_approved = 1
        wp_comment.comment_author_email = wp_user.user_email
        wp_comment.comment_author = wp_user.user_nicename

        wp_comment.addMeta('legacy_comment_id', comment.id)

        if parent != None:
            wp_comment.parent = parent

        w.session.add(wp_comment)
        w.session.commit()

        for like in comment.likes:
            awardingUser = handleUser(like.awardedBy.id)
            status = 'like'
            if like.revokedEvent_id != None:
                status = 'unlike'
            wp_comment.addLike(awardingUser.ID, status, like.creationDate)

        w.session.add(wp_comment)
        w.session.commit()

        numLikes = len([
            x for x in wp_comment.likes
            if wp_comment.likes.get(x).status == 'like'
        ])

        wp_comment.addMeta('_commentliked', numLikes)

        w.session.add(wp_comment)
        w.session.commit()

        return wp_comment
    except Exception as e:
        logging.error(e)
        return None
Exemplo n.º 3
0
def handleForumThread(threadId):
    try:
        thread = a.ForumThread.q.filter(a.ForumThread.id == threadId).first()

        thread_log.set_description_str(
            f'📕  Thread: {thread.firstPost.title[:40]}...')

        if thread != None:
            topic = w.ForoTopic.q.filter(
                w.ForoTopic.topicid == thread.id).first()
            wp_user = handleUser(thread.createdBy_id)

            if topic == None:
                topic = w.ForoTopic()

            topicExistingSlug = w.ForoTopic.q.filter(
                w.ForoTopic.slug == thread.transcription).first()
            slug = thread.transcription
            if topicExistingSlug != None:
                slug = f'{thread.transcription}-{thread.id}'

            topic.topicid = thread.id
            topic.forumid = thread.category_id
            topic.user = wp_user
            topic.created = thread.creationDate
            topic.modified = thread.modificationDate
            topic.title = thread.firstPost.title
            topic.slug = slug
            topic.status = 0

            w.session.add(topic)
            w.session.commit()

            # handle subscriptions
            for subscription in thread.subscriptions:
                logging.info(f'handle subscription {subscription.user_id}')
                wp_subscription_user = handleUser(subscription.user_id)
                wp_subscription = w.ForoSubscribe.q.filter(
                    w.ForoSubscribe.userid == wp_subscription_user.ID,
                    w.ForoSubscribe.itemid == topic.topicid,
                    w.ForoSubscribe.type == 'topic').first()
                if wp_subscription == None:
                    wp_subscription = w.ForoSubscribe()

                active = 0
                if subscription.noFurtherMails == True:
                    active = 1  # not sure but this seems to be correct TODO: verify
                wp_subscription.itemid = topic.topicid
                wp_subscription.userid = wp_subscription_user.ID
                wp_subscription.active = active
                wp_subscription.confirmkey = ''.join(
                    random.choices(string.ascii_lowercase + string.digits,
                                   k=32))
                wp_subscription.type = 'topic'

                w.session.add(wp_subscription)

            w.session.commit()

            # handleposts
            syncForumPosts(limit=10000,
                           chunksize=500,
                           query=and_(a.ForumPost.language == 'de',
                                      a.ForumPost.deleted == 0,
                                      a.ForumPost.thread_id == thread.id))
        else:
            return None
    except Exception as e:
        logging.error(e)
        return None
Exemplo n.º 4
0
def syncForums(limit=1000,
               query=and_(a.ForumCategory.deleted == 0,
                          a.ForumCategory.language == 'de')):

    apitCategories = a.ForumCategory.q.filter(query).limit(limit).all()
    maxresults = len(apitCategories)

    pbar = tqdm(total=maxresults, desc='Forums', position=0, leave=True)

    for category in apitCategories:

        forum_log.set_description_str(f'📚  Forum: {category.name[:40]}...')
        forum = w.ForoForum.q.filter(
            w.ForoForum.forumid == category.id).first()
        if forum == None:
            forum = w.ForoForum()

        parentid = category.parentCategory_id
        if parentid == None:
            parentid = 0

        permissions = phpserialize.dumps(
            ['full', 'moderator', 'standard', 'read_only', 'standard'])

        permissions = 'a:5:{i:0;s:4:"full";i:1;s:9:"moderator";i:2;s:8:"standard";i:3;s:9:"read_only";i:4;s:8:"standard";}'

        is_cat = 0

        if category.parentCategory_id == None:
            is_cat = 1

        forum.forumid = category.id
        forum.title = category.name
        forum.slug = category.transcription_transcription
        forum.description = category.description
        forum.parentid = parentid
        forum.status = 0
        forum.order = 0  # category.pos
        forum.last_topicid = 0
        forum.last_postid = 0
        forum.last_userid = 0
        # forum.last_post_date = datetime.utcnow
        forum.permissions = permissions
        forum.is_cat = is_cat

        w.session.add(forum)
        w.session.commit()

        # handle subscriptions
        for subscription in category.subscriptions:
            logging.info(f'handle forum subscription  {subscription.user_id}')
            wp_subscription_user = handleUser(subscription.user_id)
            wp_subscription = w.ForoSubscribe.q.filter(
                w.ForoSubscribe.userid == wp_subscription_user.ID,
                w.ForoSubscribe.itemid == forum.forumid,
                w.ForoSubscribe.type == 'forum').first()
            if wp_subscription == None:
                wp_subscription = w.ForoSubscribe()

            wp_subscription.itemid = forum.forumid
            wp_subscription.userid = wp_subscription_user.ID
            wp_subscription.active = 0
            wp_subscription.confirmkey = ''.join(
                random.choices(string.ascii_lowercase + string.digits, k=32))
            wp_subscription.type = 'forum'

            w.session.add(wp_subscription)

        w.session.commit()

        for progress in category.progresses:
            logging.info(f'handle visits')
            wp_progress_user = handleUser(progress.user_id)
            wp_progress = w.ForoVisit.q.filter(
                w.ForoVisit.userid == wp_progress_user.ID,
                w.ForoVisit.forumid == progress.category_id,
                w.ForoVisit.topicid == progress.lastPost.id).first()
            if wp_progress == None:
                wp_progress = w.ForoVisit()

            wp_progress.userid = wp_progress_user.ID
            wp_progress.forumid = progress.category_id
            wp_progress.topicid = progress.lastPost.id
            wp_progress.time = int(progress.lastCategoryReadDate.timestamp())
            wp_progress.name = wp_progress_user.display_name
            w.session.add(wp_progress)

        w.session.commit()

        syncThreads(limit=10000,
                    chunksize=500,
                    query=and_(a.ForumThread.language == 'de',
                               a.ForumThread.category_id == category.id))

        pbar.update(1)

    logging.info('starting syncin forums')
Exemplo n.º 5
0
def handleArticle(articleId, translation_term=None):
    try:
        logging.info(f'looking for article: {articleId}')
        article = a.Article.q.filter(a.Article.id == articleId).first()
        if article == None:
            logging.error(f'article with id:{articleId} not found')
            return

        logging.info(f'handling article:{article.title}')

        text = ' '.join([section.text for section in article.sections])

        categories = handleCategories(article)
        tags = handleTags(article)
        wp_author = handleUser(article.author.id)
        wp_post = w.Post.q.join(w.PostMeta).filter(
            w.PostMeta.meta_key == 'legacy_article_id', w.PostMeta.meta_value == f'{article.id}').first()

        if wp_post == None:
            wp_post = w.Post()
            wp_post.addMeta('legacy_article_id', article.id)

        wp_post.post_type = 'post'
        wp_post.post_title = article.title
        wp_post.post_name = slugify(article.title, separator='-')
        wp_post.post_slug = article.uri_uri.replace('/', '')
        wp_post.post_content = text
        wp_post.post_date = article.publishingDate
        wp_post.post_date_gmt = article.publishingDate.astimezone(
            timezone('GMT'))
        wp_post.post_modified = article.modificationDate

        wp_post.terms = categories + tags + getLanguage(article.language)

        post_status = 'publish'
        if article.publishingDate > datetime.now():
            post_status = 'future'
            # add cron option for future

        wp_post.author = wp_author
        wp_post.post_status = post_status

        previewImage = image = article.previewImageLegacy or article.heroImage or article.previewImage
        if previewImage != None:

            featureMedia = w.Post.q.join(w.PostMeta).filter(w.Post.post_type == 'attachment',
                                                            w.PostMeta.meta_key == 'legacy_userfile_id', w.PostMeta.meta_value == f'{previewImage.id}').first()
            imageCreator = handleUser(previewImage.user.id)
            if featureMedia == None:
                props = {
                    "meta": {"legacy_userfile_id": f'{previewImage.id}'},
                    "author": imageCreator.ID
                }
                featureMediaId = w.createMediaFromUrl(
                    previewImage.url, previewImage.mimeType, props=props)
            else:
                featureMediaId = featureMedia.ID
            wp_post.addMeta('_thumbnail_id', f'{featureMediaId}')

        w.session.add(wp_post)
        w.session.commit()

        if translation_term != None:
            wp_post.terms = wp_post.terms + [translation_term]

            w.session.add(wp_post)
            w.session.commit()
        # recursively handle translations
        if len(article.translations) > 0:
            nt = w.Term()
            tid = uniqid('pll_')
            nt.taxonomy = 'post_translations'
            nt.slug = tid
            nt.name = tid
            wp_post.terms = wp_post.terms + [nt]

            w.session.add(nt)
            w.session.add(wp_post)
            w.session.commit()
            ntdesc = {article.language: wp_post.ID}
            for translation in article.translations:
                wp_translation = handleArticle(
                    translation.id, translation_term=nt)

                if wp_translation != None:
                    postlang = [
                        lang for lang in wp_translation.terms if lang.taxonomy == 'language']

                    if len(postlang) > 0:
                        ntdesc[f'{postlang[0].slug}'] = wp_translation.ID
                    else:
                        print('ERROR', translation.language, 'not defined')

            nt.description = phpserialize.dumps(ntdesc)
            w.session.add(nt)
            w.session.commit()

        return wp_post
    except Exception as e:
        logging.error(e)
        return None