def test_term_lifecycle(self): term = WordPressTerm() term.name = 'Test Term' term.taxonomy = 'category' # create the term term_id = self.client.call(taxonomies.NewTerm(term)) self.assertTrue(term_id) term.id = term_id # re-fetch to verify term2 = self.client.call(taxonomies.GetTerm(term.taxonomy, term.id)) self.assertEqual(term.name, term2.name) # set a description and save term.description = "My test term" response = self.client.call(taxonomies.EditTerm(term.id, term)) self.assertTrue(response) # re-fetch to verify term3 = self.client.call(taxonomies.GetTerm(term.taxonomy, term.id)) self.assertEqual(term.description, term3.description) # delete the term response = self.client.call(taxonomies.DeleteTerm(term.taxonomy, term.id)) self.assertTrue(response)
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))
def test_term_parent_child(self): parent = WordPressTerm() parent.taxonomy = 'category' parent.name = 'Test Parent Term' parent_id = self.client.call(taxonomies.NewTerm(parent)) self.assertTrue(parent_id) parent.id = parent_id child = WordPressTerm() child.taxonomy = parent.taxonomy child.name = 'Test Child Term' child.parent = parent.id child_id = self.client.call(taxonomies.NewTerm(child)) self.assertTrue(child_id) child.id = child_id try: # re-fetch to verify child2 = self.client.call(taxonomies.GetTerm(child.taxonomy, child.id)) self.assertEqual(child.parent, child2.parent) finally: # cleanup self.client.call(taxonomies.DeleteTerm(child.taxonomy, child.id)) self.client.call(taxonomies.DeleteTerm(parent.taxonomy, parent.id))
def insert_post(self, joomla_post, category_id): client = self.get_client() post = WordPressPost() post.title = joomla_post.get_title() text_to_replace = joomla_post.get_introtext() text_to_replace = str(text_to_replace).replace("href=\"images/", "href=\"/images/") post.content = str(text_to_replace).replace("src=\"images/", "src=\"/images/") post.date = joomla_post.get_created() post.thumbnail = self.post_thumbnail(joomla_post.get_id())['id'] post.post_status = "publish" category = client.call(taxonomies.GetTerm('category', category_id)) post.terms.append(category) client.call(NewPost(post)) return post
def update_post_wp(self, wp_post_id): """ Update post with id in WordPress. :param wp_post_id: post id """ post = self.client.call(posts.GetPost(wp_post_id)) if not post: raise ResourceError("Post not found, id: {}.".format(wp_post_id)) key_assoc = { "city": "city_id", "coordinates_lat": "latitude", "coordinates_long": "longitude", "geo_hash_id": "geo_hash_id", "rate": "rate", "place_id": "place_id", "address": "address_coord" } post_custom_fields = CustomFields(post.custom_fields, key_assoc) custom_fields_unpacked = post_custom_fields.unpack() try: location = [] location_unserialized = custom_fields_unpacked["city_id"] for loc in location_unserialized: term = self.client.call( taxonomies.GetTerm("locations", int(loc))) location.append(term) break except KeyError: pass db_post = self.connection.get( InterestPoint, { "latitude": custom_fields_unpacked["latitude"], "longitude": custom_fields_unpacked["longitude"] }) if not db_post: db_post = self.connection.get(InterestPoint, {"title": post.title}) if not db_post: raise MissingResourceError( "InterestPoint not found for post with id: {}.".format( post.id)) dict_post = post.__dict__() post_diff = self.prejudice_diff(post, db_post) new_post_data = WordPressPost() new_post_data.custom_fields = [] self.client.call(posts.EditPost(post.id, new_post_data)) exit()
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
def test_get_term(self): term = self.client.call(taxonomies.GetTerm('category', 1)) self.assertTrue(isinstance(term, WordPressTerm))
post_category_id = 0 for val in taxes: if str(val) == str(type): is_present = 1 post_category_id = val.id break if is_present == 0: tag = WordPressTerm() tag.taxonomy = 'job_listing_type' tag.name = type post_category_id = wp.call(taxonomies.NewTerm(tag)) category = wp.call( taxonomies.GetTerm('job_listing_type', post_category_id)) post = wp.call(posts.GetPost(new_job.id)) post.terms.append(category) wp.call(posts.EditPost(post.id, post)) attachment_id = 0 if logo: print "https://www.mustakbil.com" + str(logo) img_data = requests.get("https://www.mustakbil.com" + str(logo)).content with open('file01.jpg', 'wb') as handler: handler.write(img_data)
is_present = 0 post_category_id = 0 for val in taxes: if str(val) == str(industry): is_present = 1 post_category_id = val.id break if is_present == 0: tag = WordPressTerm() tag.taxonomy = 'company_category' tag.name = industry post_category_id = wp.call(taxonomies.NewTerm(tag)) category = wp.call(taxonomies.GetTerm('company_category', post_category_id)) post = wp.call(posts.GetPost(new_company.id)) post.terms.append(category) wp.call(posts.EditPost(post.id, post)) attachment_id = 0 if logo: print "https://www.mustakbil.com"+str(logo) img_data = requests.get("https://www.mustakbil.com"+str(logo)).content with open('file01.jpg', 'wb') as handler: handler.write(img_data)