コード例 #1
0
    def createIt(userFile):
        try:
            # logging.info('creating mediafile: %s', userFile.fileName)
            mediaSrc = requests.get(userFile.url)

            # print(mediaSrc.content)
            headers = {
                'cache-control': 'no-cache',
                "Content-Disposition":
                f'attachment; filename="{userFile.fileName}"',
                'content-type': 'image/jpeg'
            }
            # print('headers', headers)
            res = api.post('/media', headers=headers, data=mediaSrc.content)
            mediaResponse = json.loads(res.text)
            mediaId = mediaResponse['id']

            mediaPayload = {"meta": {"legacy_userfile_id": f'{userFile.id}'}}

            updateres = api.put(f'/media/{mediaId}',
                                data=json.dumps(mediaPayload),
                                headers={'content-type': 'application/json'})

            # print(updateres)
            # print(mediaId)
            pbar.update(1)  # one file created
            return mediaId

        except Exception as e:
            logging.error('error on creating media: %s', e)
            pass
コード例 #2
0
 def createIt(tagName):
     tag = tagName.capitalize()
     tagSlug = slugify(tag, separator="-")
     try:
         logging.info('creating tag: %s', str(tagName))
         return api.post('/tags', data={
             'name': tag,
             'slug': tagSlug
         }).json()
     except Exception as e:
         logging.error('error on creating tag: %s', e)
         pass
コード例 #3
0
    def createIt(article):
        text = getTextForArticleId(article.id)
        author = next((author for author in authors if author.id ==
                       article.createdBy_id), None)
        createdById = wp_users.loc[f'{author.id}'].ID

        categoryIds = [
            str(wp_categories.loc[category].term_id) for category in extractCategories(article)]
        tagIds = [str(wp_tags.loc[tag].term_id) for tag in extractArticleTags(
            article, devices)]

        featureImage = next(
            (img for img in featureImages if img.id == article.id), None)

        if featureImage is not None:
            try:
                featureMedia = wp_mediafiles.loc[f'{featureImage.id}']
                featureMediaId = int(featureMedia.ID)
            except:
                featureMedia = None
                featureMediaId = 0
        else:
            featureMedia = None
            featureMediaId = 0

        postPayload = {
            "title": article.title,
            "slug": article.uri_uri[1:-1],
            "content": text,
            "featured_media": featureMediaId,
            "author": int(createdById),
            "status": "publish",
            "categories": ",".join(categoryIds),
            "tags": ",".join(tagIds),  # something is wrong with this
            "date": pd.to_datetime(str(article.publishingDate)),
            "meta": {
                "legacy_article_id": f'{article.id}'
            }
        }

        jsonstr = json.dumps(postPayload, default=str)

        try:

            res = api.post('/posts', data=jsonstr,
                           headers={'content-type': 'application/json'})
            # print(res)
            pbar.update(1)
            return res
            # print('OK')
        except Exception as e:
            # print(jsondata)
            print(e)
コード例 #4
0
 def createIt(categoryName):
     category = categoryName.capitalize()
     categorySlug = slugify(categoryName, separator="-")
     try:
         logging.info('creating category: %s', category)
         return api.post('/categories', data={
             'name': category,
             'slug': categorySlug
         }).json()
     except Exception as e:
         logging.error('error on creating category: %s', e)
         pass
コード例 #5
0
    def createIt(author):
        if author.staffPageDescriptionJson is not None:
            description = json.loads(author.staffPageDescriptionJson)
        else:
            description = json.loads('{"de": ""}')

        if author.emailAddressNew is not None:
            email = author.emailAddressNew
        else:
            email = author.emailAddress

        email = re.sub(r"_DA_\d*$", "", email)
        # print('handle', email)

        name = author.communityName.split(' ')

        payload = {
            "username": slugify(author.username, separator="_"),
            "name": author.communityName,
            "first_name": name[0],
            "last_name": name[1] if len(name) > 1 else '',
            "roles": ["author"],
            "email": email,
            "description": description.get('de'),
            "locale": "en_US",
            "nickname": "",
            "password": "******",
            "meta": {
                "legacy_user_id": f'{author.id}'
            }
        }
        try:
            logging.info('creating author: %s', email)
            res = api.post('/users',
                           data=json.dumps(payload),
                           headers={'content-type': 'application/json'})
            return res
        except Exception as e:
            logging.error('error on creating author: %s (%s)', email, e)
            pass