def sourcename(url, cat1=None, cat2=None, cat3=None, d=True):
    html = getPage(url, "page1.html")
    os.remove('pages/page1.html')
    title = html.select('h1')[0].text
    first_para = html.select('.story-content > p:nth-of-type(1)')[0].text
    second_para = html.select('.story-content > p:nth-of-type(2)')[0].text
    try:
        image = html.select('.image > img')[0].get('src')
    except Exception:
        image = None
    wp = Client('http://www.domain.com/xml-rpc.php', 'username', 'password')
    if image:
        filename = 'http://www.livemint.com' + image
        path = os.getcwd() + "\\00000001.jpg"
        f = open(path, 'wb')
        f.write(urllib.urlopen(filename).read())
        f.close()
        # prepare metadata
        data = {
            'name': 'picture.jpeg',
            'type': 'image/jpeg',  # mimetype
        }

        with open(path, 'rb') as img:
            data['bits'] = xmlrpc_client.Binary(img.read())
        response = wp.call(media.UploadFile(data))
    post = WordPressPost()
    post.title = title
    post.user = 14
    post.post_type = "post"
    post.content = first_para + '\n' + '\n' + second_para
    if d:
        post.post_status = "draft"
    else:
        post.post_status = "publish"
    if image:
        attachment_id = response['id']
        post.thumbnail = attachment_id
    post.custom_fields = []
    post.custom_fields.append({
        'key': 'custom_source_url',
        'value': url
    })
    if cat1:
        cat1 = wp.call(taxonomies.GetTerm('category', cat1))
        post.terms.append(cat1)
    if cat2:
        cat2 = wp.call(taxonomies.GetTerm('category', cat2))
        post.terms.append(cat2)
    if cat3:
        cat3 = wp.call(taxonomies.GetTerm('category', cat3))
        post.terms.append(cat3)
    addpost = wp.call(posts.NewPost(post))
Beispiel #2
0
 def process_item(self, item, spider):
     wp = Client('https://www.along.party/xmlrpc.php', '', '')
     post = WordPressPost()
     post.title = item['title']
     post.user = item['author']
     post.link = item['url']
     # post.date = item['publish_time']
     post.content = item['body']
     post.content = u"%s \n 本文转载自 <a href='%s'> %s</a> " % (
         item['body'], item['url'], item['title'])
     post.post_status = 'publish'
     post.terms_names = {'post_tag': 'Python', 'category': 'Python'}
     wp.call(NewPost(post))
Beispiel #3
0
def create_wordpress_draft(publish_target, title, html, tags):
  post = WordPressPost()
  today = datetime.date.today()
  post.title = title
  post.content = html
  client = Client( publish_target["url"] + "/xmlrpc.php",  publish_target["username"],  publish_target["password"])
  category = client.call(taxonomies.GetTerm('category',  publish_target["default_category_id"]))
  post.terms.append(category)
  post.user = publish_target["default_user_id"]
  post.terms_names = {'post_tag': tags}
  post.comment_status = 'open'
  post.id = client.call(posts.NewPost(post))
  return post
Beispiel #4
0
def sourcename(url, cat1=None, cat2=None, cat3=None, d=True):
    html = getPage(url, "page1.html")
    os.remove('pages/page1.html')
    title = html.select('h1')[0].text
    first_para = html.select('.story-content > p:nth-of-type(1)')[0].text
    second_para = html.select('.story-content > p:nth-of-type(2)')[0].text
    try:
        image = html.select('.image > img')[0].get('src')
    except Exception:
        image = None
    wp = Client('http://www.domain.com/xml-rpc.php', 'username', 'password')
    if image:
        filename = 'http://www.livemint.com' + image
        path = os.getcwd() + "\\00000001.jpg"
        f = open(path, 'wb')
        f.write(urllib.urlopen(filename).read())
        f.close()
        # prepare metadata
        data = {
            'name': 'picture.jpeg',
            'type': 'image/jpeg',  # mimetype
        }

        with open(path, 'rb') as img:
            data['bits'] = xmlrpc_client.Binary(img.read())
        response = wp.call(media.UploadFile(data))
    post = WordPressPost()
    post.title = title
    post.user = 14
    post.post_type = "post"
    post.content = first_para + '\n' + '\n' + second_para
    if d:
        post.post_status = "draft"
    else:
        post.post_status = "publish"
    if image:
        attachment_id = response['id']
        post.thumbnail = attachment_id
    post.custom_fields = []
    post.custom_fields.append({'key': 'custom_source_url', 'value': url})
    if cat1:
        cat1 = wp.call(taxonomies.GetTerm('category', cat1))
        post.terms.append(cat1)
    if cat2:
        cat2 = wp.call(taxonomies.GetTerm('category', cat2))
        post.terms.append(cat2)
    if cat3:
        cat3 = wp.call(taxonomies.GetTerm('category', cat3))
        post.terms.append(cat3)
    addpost = wp.call(posts.NewPost(post))
Beispiel #5
0
def create_wordpress_draft(publish_target, title, html, tags):
    post = WordPressPost()
    today = datetime.date.today()
    post.title = title
    post.content = html
    client = Client(publish_target["url"] + "/xmlrpc.php",
                    publish_target["username"], publish_target["password"])
    category = client.call(
        taxonomies.GetTerm('category', publish_target["default_category_id"]))
    post.terms.append(category)
    post.user = publish_target["default_user_id"]
    post.terms_names = {'post_tag': tags}
    post.comment_status = 'open'
    post.id = client.call(posts.NewPost(post))
    return post
Beispiel #6
0
def publish(pic, title, content):
    '''
    publish a post and set to open to comment and ping(trackback)
    '''
    attachment = upload(pic)
    wp_client = Client(rpc_service_url, user, password)
    post = WordPressPost()
    post.title = title
    post.content = "%s\n\n<a href='%s'><img class='alignnone size-full wp-image-%s' alt='%s' src='%s' /></a>" % (content, attachment['url'], attachment['id'], attachment['file'], attachment['url'])
    #post.tags='test, test2'
    #post.categories=['pet','picture']
    post.thumbnail = attachment['id']
    #change status to publish
    post.id = wp_client.call(posts.NewPost(post))
    post.post_status = 'publish'
    post.comment_status = 'open'
    post.ping_status = 'open'
    post.user = account_ids[r.randint(0, len(account_ids)-1)]
    wp_client.call(posts.EditPost(post.id, post))
    return post.id
Beispiel #7
0
def _add_blogpost(post_data):
    '''
    Adds a blog post parsed from blog.blogpost

    Model published status codes are as follows:  PUBLISHED = 1, DRAFT = 2, DELETED = 3

    :param post_data: Python dict of post structure.  See modelsamples/blogpost_sample.txt for structure.
    :return:
    '''
    post = WordPressPost()
    post.post_type = 'news'
    post.title = post_data['fields']['title']
    post.content = post_data['fields']['content']
    post.date = _return_datetime(post_data['fields']['publish_date'])
    post.date_modified = _return_datetime(post_data['fields']['updated'])
    post.slug = post_data['fields']['slug']
    if post_data['fields']['status'] == 2:
        post.post_status = 'publish'
    # Assign Author
    if post_data['fields']['user']:
        wp_userid = _get_wordpress_user_id_by_email(
            _get_django_user_email_by_id(post_data['fields']['user']))
        if wp_userid:
            post.user = wp_userid
    # TODO set catagories and tags to proper taxonomy
    # post.terms_names = {
    #     'category': ['Blogpost']
    # }
    # if post_data['fields']['categories']:
    #     categories = []
    #     for category in dp.get_content(DJANGO_DATA, 'blog.blogcategory'):
    #         if category['pk'] in post_data['fields']['categories']:
    #             categories.append(category['fields']['title'])
    #     post.terms_names['post_tag'] = categories
    try:
        if post_data['fields']['status'] != 3:
            post.id = API.call(NewPost(post))
            print("created post", post.id, post.title)
    except Fault as err:
        pprint(post)
        print(err.faultCode, err.faultString)
Beispiel #8
0
def entry_to_wppost(entry, client):
    post             = WordPressPost()
    # Convert entry values to Post value
    post.user        = get_author_by_display_name(entry.author, client)
    post.date        = entry.published_parsed
    post.post_status = "draft"
    post.title       = entry.title
    post.content     = entry.content[0].value
    post.excerpt     = entry.summary
    post.link        = entry.link
    # There is some given tags
    if len(entry.tags):
        entry.tags = [t.term for t in entry.tags]
        # Add category (with the first tag)
        post.terms_names = {
            'category': entry.tags[0:1],
            'post_tag': [],
        }
        # Add tags
        if len(entry.tags) > 1: post.terms_names['post_tag'] = entry.tags[1:]
    return post
Beispiel #9
0
def _add_application(app_data):
    '''
    Adds an application parse from apps.application

    :param app_data: Python dict of application structure. See modelsamples/application_sample.txt
    :return:
    '''
    post = WordPressPost()
    post.post_type = 'application'
    post.title = app_data['fields']['name']
    post.slug = app_data['fields']['slug']
    post.content = _format_app_content(app_data)
    post.date = _return_datetime(app_data['fields']['created'])
    post.date_modified = _return_datetime(app_data['fields']['updated'])

    # TODO assign to proper taxonomies once those are in.

    # Assign Author
    if app_data['fields']['owner']:
        wp_userid = _get_wordpress_user_id_by_email(
            _get_django_user_email_by_id(app_data['fields']['owner']))
        if wp_userid:
            post.user = wp_userid

    # Assign Categories and Tags
    post.terms_names = _parse_taxonomies(app_data)

    # Put the previous page url in a custom field.
    legacy_url = "https://www.us-ignite.org/apps/%s/" % (
        app_data['fields']['slug'])
    post.custom_fields = [{'key': 'legacy_url', 'value': legacy_url}]

    # Set publish status and push to site
    if app_data['fields']['status'] == 1:
        post.post_status = 'publish'
    try:
        if app_data['fields']['status'] != 3:
            post.id = API.call(NewPost(post))
    except Fault as err:
        pprint(err.faultString)
def create_blog_post(message):
    pprint(message['username'] + str(message['message_id']))
    username = message['username']
    if username in username_converts: username = username_converts[username]
    url = "http://" + username + ".plataforma.cc/xmlrpc.php"
    client = Client(url, admin_user, admin_pass)

    post = WordPressPost()

    if not username in username_ids:
        blogusers = client.call(users.GetUsers())
        username_ids[username] = next(u for u in blogusers
                                      if u.username == username).id

    post.user = username_ids[username]
    post.title = ''
    post.content = message['text']
    post.post_status = 'publish'

    if len(message['photo']):
        if post.content.find('[plataformacc_gallery]') == -1:
            post.content = post.content + '[plataformacc_gallery]'
            if message['caption']:
                post.content = post.content + message[
                    'caption'] + '[/plataformacc_gallery]'
    elif not len(message['text']):
        data = dict(id=str(message['message_id'][-1]),
                    channel=message['username'])
        post.content = Template(
            '[telegram_embed id="$id" channel="$channel"]').substitute(data)

    post_id = client.call(GetPostByTelegramID(message['message_id']))

    if (post_id == -1):
        print('multiple, wont update')
        return

    if (post_id == -2):
        print('has images, wont update')
        return

    if post_id and not message['edit']:
        print('already exists')
        return

    if not post_id:
        print('adding new')
        post_id = client.call(posts.NewPost(post))
        post.custom_fields = []
        for telegram_id in message['message_id']:
            post.custom_fields.append({
                'key': 'telegram_id',
                'value': telegram_id
            })
            print('custom field', {'key': 'telegram_id', 'value': telegram_id})

    if len(message['photo']):
        for photo in message['photo']:
            mimetype = mime.from_file(photo)
            ext = mimetype.split('/')[-1]
            data = {
                'name': photo + '.' + ext,
                'type': mimetype,  # mimetype
                'post_id': post_id,
            }
            with open(photo, 'rb') as img:
                data['bits'] = xmlrpc_client.Binary(img.read())
            response = client.call(media.UploadFile(data))
            post.wp_post_thumbnail = response['id']
            os.remove(photo)
        print('calling edit for photos', post_id)
        print(post)
    print('Updating', post_id, post)
    client.call(posts.EditPost(post_id, post))
Beispiel #11
0
                continue
            if not tag: continue
            if getImageSrc(tag) != '':
                img_id = imageUpload(getImageSrc(tag), alias, i=i)
            else:
                continue
            img_tag = '<!-- wp:image {"id":%s,"sizeSlug":"large"} --><figure class="wp-block-image size-large"><img src="%s" alt="" class="wp-image-%s"/></figure><!-- /wp:image -->' % (
                img_id['id'], img_id['link'], img_id['id'])
            content += img_tag
            i += 1
            continue

        try:
            content += tag.prettify()
        except Exception as e:
            print('Exception: %s' % e)

    img_id = imageUpload(image, alias)

    post = WordPressPost()
    post.title = soup.find(class_='article__title').text
    post.content = content
    post.mime_type = "text/html"
    post.user = 2  # WP Author ID
    post.link = alias
    post.post_type = "post"
    post.thumbnail = img_id['id']
    post.post_status = "publish"  # "draft"
    post_id = client.call(posts.NewPost(post))
    print("Post: %s (%s) Done!" % (post_id, alias))