def new_post(self, title, content, tags, attachment_id): post = WordPressPost() post.title = title post.content = content post.terms = tags post.thumbnail = attachment_id post.post_status = 'publish' post.id = self.client.call(posts.NewPost(post))
def addItem(self, item): # 跳转链作为唯一标识去重 # exist_posts = self.rpcClient.call(posts.GetPosts({'custom_fields':{'key':'link_value', 'value' : item.link}})) if self.itemExist(item.link): return print 'item.link = ' + item.link res = urlparse.urlparse(item.thumb) if len(res.query) > 0: qs = urlparse.parse_qs(res.query) # huihui图片ID des = qs['id'][0] filename = des + '.png' destfile = 'temp/' + filename else: filename = item.thumb[item.thumb.rfind('/'):] destfile = 'temp' + filename downloadimage(item.thumb, destfile) # prepare metadata data = { 'name': filename, 'type': 'image/png', # mimetype } # read the binary file and let the XMLRPC library encode it into base64 with open(destfile, 'rb') as img: data['bits'] = xmlrpc_client.Binary(img.read()) response = self.rpcClient.call(media.UploadFile(data)) attachment_id = response['id'] post = WordPressPost() post.title = item.title post.content = item.content post.thumbnail = attachment_id post.custom_fields = [{'key': 'link_value', 'value': item.link}] post.id = self.rpcClient.call(posts.NewPost(post)) # # attachment_id.parentid = post.id # # whoops, I forgot to publish it! post.post_status = 'publish' # cats = self.rpcClient.call(taxonomies.GetTerms('category')) for cat in self.categorys: if cat.name == item.category: post.terms = [cat] break self.rpcClient.call(posts.EditPost(post.id, post))
def addItem(self, item): # 跳转链作为唯一标识去重 # exist_posts = self.rpcClient.call(posts.GetPosts({'custom_fields':{'key':'link_value', 'value' : item.link}})) if self.itemExist(item.link): return print 'item.link = '+item.link res = urlparse.urlparse(item.thumb) if len(res.query) > 0: qs = urlparse.parse_qs(res.query) # huihui图片ID des = qs['id'][0] filename = des+'.png' destfile = 'temp/'+filename else: filename = item.thumb[item.thumb.rfind('/'):] destfile = 'temp'+filename downloadimage(item.thumb,destfile) # prepare metadata data = { 'name': filename, 'type': 'image/png', # mimetype } # read the binary file and let the XMLRPC library encode it into base64 with open(destfile, 'rb') as img: data['bits'] = xmlrpc_client.Binary(img.read()) response = self.rpcClient.call(media.UploadFile(data)) attachment_id = response['id'] post = WordPressPost() post.title = item.title post.content = item.content post.thumbnail = attachment_id post.custom_fields = [{'key':'link_value', 'value' : item.link}] post.id = self.rpcClient.call(posts.NewPost(post)) # # attachment_id.parentid = post.id # # whoops, I forgot to publish it! post.post_status = 'publish' # cats = self.rpcClient.call(taxonomies.GetTerms('category')) for cat in self.categorys: if cat.name == item.category: post.terms = [cat] break self.rpcClient.call(posts.EditPost(post.id, post))
def uploadPost(postType, artist, album, song, artwork, connection): albumType, releaseDate = getInfo(artist, album) post = WordPressPost() post.post_type = 'download' if postType == 'bundle': post.title = album else: post.title = song post.content, keyword = getContent(artist, album, song) post.date = datetime.datetime.strptime(releaseDate, '%Y.%m.%d') post.terms = wp.call( taxonomies.GetTerms('download_artist', {'search': artist})) post.thumbnail = artwork post.custom_fields = [] post.post_status = 'publish' post.custom_fields.append({'key': 'year', 'value': releaseDate}) post.custom_fields.append({'key': 'music_type', 'value': postType}) post.id = wp.call(posts.NewPost(post)) with connection.cursor() as cursor: if postType == 'bundle': sql = 'INSERT INTO `upload_info` (`item_type`, `album_type`, `artist`, `album`, `post_title`, `release_date`, `thumbnail_id`, `post_id`, `published`, `updated_at`) VALUES (%s, %s, %s,%s,%s,%s,%s,%s,%s, now())' cursor.execute(sql, (postType, albumType, artist, album, post.title, post.date, post.thumbnail, post.id, '1')) else: sql1 = 'INSERT INTO `upload_info` (`item_type`, `album_type`, `artist`, `album`, `song`, `post_title`, `release_date`, `thumbnail_id`, `post_id`, `published`, `updated_at`) VALUES (%s, %s, %s,%s,%s,%s,%s,%s,%s,%s, now())' sql2 = 'INSERT INTO `wp9r_postmeta` (`post_id`, `meta_key`, `meta_value`) VALUES (%s, "_yoast_wpseo_focuskw_text_input", %s)' sql3 = 'INSERT INTO `wp9r_postmeta` (`post_id`, `meta_key`, `meta_value`) VALUES (%s, "_yoast_wpseo_focuskw", %s)' cursor.execute( sql1, (postType, albumType, artist, album, song, post.title, post.date, post.thumbnail, post.id, '1')) cursor.execute(sql2, (post.id, keyword)) cursor.execute(sql3, (post.id, keyword)) if postType == 'bundle': print('Upload Successful for album %s - %s. Post id = %s' % (artist, album, post.id)) else: print('Upload Successful for song %s - %s. Post id = %s' % (artist, song, post.id)) return post.id
def create_match_post(self, media_post, title, tags, extra): post = WordPressPost() post.title = title if extra == None: extra = "" post.content = media_post.get_embed() + extra post.terms_names = { 'post_tag': tags } post.terms = self.find_terms() post.post_status = 'publish' post.custom_fields = [ { 'key': 'thumbnail', 'value': media_post.get_thumb() }, { 'key': 'event_date', 'value': self.date } ] post.id = self.client.call(posts.NewPost(post)) return post
def send_post_map(self, data): nom = data.get('nom') url_log_pas = data.get('url') post_text = data.get('post') # новые поля title = data.get('title') date_time = data.get('date') category = data.get('category') # для тестов #date_time = datetime.datetime(2020, 6, 22) #category = 'test categoryes' url, user, password = url_log_pas.split(';') try: if self.proxy != 'None': wp = Client(url + '/xmlrpc.php', user, password,transport=self.transport) else: wp = Client(url+'/xmlrpc.php', user,password) except: #self.logger.exception('Client') self.logger.info(f'{nom}:{url} - {Fore.RED}failed{Style.RESET_ALL}') return {'url_log_pas':url_log_pas,'url':url,'post_id':False} try: post = WordPressPost() post.mime_type = "text/html" if title: post.title = title else: post.title = '' post.content = post_text if date_time: post.date = datetime.datetime.strptime(date_time,'%d.%m.%Y %H:%M') if category: # добавляем категорию categories = self.get_category(wp, category) if categories: post.terms = categories else: self.logger.info(f'{category} dont created') post.post_status = 'publish' try: post.id= wp.call(NewPost(post)) except (InvalidCredentialsError, ServerConnectionError, ServerConnectionError, XmlrpcDisabledError, socket.timeout): self.logger.info(f'{nom}:{url} - {Fore.RED}failed{Style.RESET_ALL}') return {'url_log_pas':url_log_pas,'url': url, 'post_id': False} # пока не решил брать отсюда урл или нет #post_new = wp.call(GetPost(post.id)) self.logger.info(f'{nom}:{url} send post! {Fore.GREEN}Post urls {url}/?p={post.id}{Style.RESET_ALL}') return {'url_log_pas':url_log_pas,'url': url, 'post_id': post.id} except Exception as e: try: faultCode = e.faultCode faultString = e.faultString.encode ("utf-8") self.logger.info(f'{Fore.RED}{nom}: Error send post {url} {faultCode} {faultString} {Style.RESET_ALL}') return {'url_log_pas': url_log_pas, 'url': url, 'post_id': False} except: return {'url_log_pas': url_log_pas, 'url': url, 'post_id': False}
def main(): args = sys.argv[1:] if len(args) != 2: print( 'Usage: ./ssp_se_load_roster.py <Wordpress xmlrpc url> <csvfile>') sys.exit(1) xmlrpc = args[0] csvfile_name = args[1] if not os.path.isfile(csvfile_name): print('I can\'t find the file: ' + csvfile_name) sys.exit(1) username = raw_input('Enter your WordPress user name: ') password = getpass() wp = Client(xmlrpc, username, password) listofseriesterms = wp.call(GetTerms('series')) print('To which series are you going to add all these posts? (id, name)') series = {} for seriesterm in listofseriesterms: print(seriesterm.id + ', ' + seriesterm.name) series[seriesterm.id] = seriesterm.name main_series_termid = raw_input('Please enter the id: ') if not main_series_termid in series: print('That series id does not exist') sys.exit(1) else: print(series[main_series_termid] + ' series selected.') child_series = {} for seriesterm in listofseriesterms: if seriesterm.parent == main_series_termid: child_series[seriesterm.name.lower()] = seriesterm.id print('child_series') print(child_series) existing_posts = wp.call(GetPosts({ 'post_type': 'podcast', 'number': 9999 })) existing_series_posts = {} child_series_existing_episode_numbers = {} for post in existing_posts: post_terms = [] for term in post.terms: if term.id in child_series.values( ) or term.id == main_series_termid: post_terms.append(term.id) if post_terms: for customfield in post.custom_fields: if customfield['key'] == 'date_recorded' and customfield[ 'value']: date_recorded_string = customfield['value'] elif customfield['key'] == 'episode_number' and customfield[ 'value']: for post_term_id in post_terms: if post_term_id in child_series_existing_episode_numbers: child_series_existing_episode_numbers[ post_term_id].append(int(customfield['value'])) else: child_series_existing_episode_numbers[ post_term_id] = [int(customfield['value'])] date_recorded = getdatetime(date_recorded_string) existing_series_posts[date_recorded] = (post.id, post_terms) #open and parse the csv replace_all = False replace_none = False fieldname_translation = { 'date': 'date_recorded', 'title': 'title', 'series': 'series', 'track': 'episode_number', 'passage': 'bible_passage', 'preacher': 'preacher', 'comments': 'content', 'time': 'time' } list_from_csv = {} with open(csvfile_name, 'r') as csvfile: reader = csv.DictReader(csvfile) for messyentry in reader: entry = {} for entrykey, entryval in messyentry.iteritems(): entry[fieldname_translation[entrykey.lower()]] = entryval if not (('date_recorded' in entry) or ('preacher' in entry)): continue if entry['date_recorded'] == '' or entry['preacher'] == '': continue csvdate = getdatetime(entry['date_recorded']) if csvdate == None: continue entry['date_recorded'] = csvdate.strftime('%d-%m-%Y') try: int(entry['episode_number']) except (ValueError, KeyError): entry['episode_number'] = '' for fieldname in fieldname_translation.values(): if not fieldname in entry: entry[fieldname] = '' if not entry['series']: entry['series'] = 'one-off' if (csvdate in existing_series_posts): if not (replace_none or replace_all): confirmation_raw_in = raw_input( 'There is already a podcast in this series with date ' + csvdate.strftime('%d %b %Y') + ', replace it? \n(Y/N/All/None)\n').lower() elif replace_none: continue if confirmation_raw_in == 'none': replace_none = True if not confirmation_raw_in in ['y', 'all']: continue if confirmation_raw_in == 'all': replace_all = True if wp.call(DeletePost(existing_series_posts[csvdate][0])): print('Deleted old post for ' + csvdate.strftime('%d %b %Y') + '.') entry['bible_passage'] = cleanUpScripture(entry['bible_passage']) list_from_csv[csvdate] = entry template_settings = { 'ep_title_template': 'title', 'service_time': 'time', 'comments_template': 'content' } #work out series and episode_numbers: for seriestermid in child_series_existing_episode_numbers: try: child_series_existing_episode_numbers[seriestermid] = sorted( child_series_existing_episode_numbers[seriestermid], reverse=True)[0] except IndexError as e: print(e) try: child_series_existing_episode_numbers[seriestermid] = int( series[seriestermid].count) except: child_series_existing_episode_numbers[seriestermid] = 0 prefix = 'ss_podcasting_data_' termidlist = ['0', main_series_termid] optionslist = [] templates_per_series = {} for child_series_termid in child_series.values(): termidlist.append(child_series_termid) for termid_item in termidlist: templates_per_series[termid_item] = {} if termid_item == '0': suffix = '' else: suffix = '_' + termid_item for eachsetting in template_settings: optionslist.append(prefix + eachsetting + suffix) list_of_WPOptions = wp.call(GetOptions(optionslist)) for wp_Option in list_of_WPOptions: if wp_Option.name[len(prefix):] in template_settings: templates_per_series['0'][ wp_Option.name[len(prefix):]] = wp_Option.value else: for termid_item in termidlist: suffix = '_' + termid_item if wp_Option.name[-len(suffix):] == suffix: templates_per_series[termid_item][wp_Option.name[ len(prefix):-len(suffix)]] = wp_Option.value timezone = get_localzone() for entry, details in sorted(list_from_csv.iteritems()): if not details['series'].lower() in child_series: wpseries = WordPressTerm() wpseries.taxonomy = 'series' wpseries.name = details['series'] wpseries.parent = main_series_termid wpseries.id = wp.call(NewTerm(wpseries)) child_series[details['series']] = wpseries.id listofseriesterms.append(wpseries) series[wpseries.id] = wpseries.name child_series[wpseries.name.lower()] = wpseries.id if details['episode_number'] == '': details['episode_number'] = '1' child_series_existing_episode_numbers[wpseries.id] = 1 else: child_series_existing_episode_numbers[wpseries.id] = int( details['episode_number']) details['seriesid'] = wpseries.id else: try: child_series_existing_episode_numbers[child_series[ details['series'].lower()]] except KeyError: for seriesterm in listofseriesterms: if seriesterm.id == child_series[ details['series'].lower()]: child_series_existing_episode_numbers[child_series[ details['series'].lower()]] = seriesterm.count else: child_series_existing_episode_numbers[child_series[ details['series'].lower()]] = 0 if details['episode_number'] == '': child_series_existing_episode_numbers[child_series[ details['series'].lower()]] += 1 details['episode_number'] = str( child_series_existing_episode_numbers[child_series[ details['series'].lower()]]) else: child_series_existing_episode_numbers[child_series[ details['series'].lower()]] = int( details['episode_number']) details['seriesid'] = child_series[details['series'].lower()] for template_setting, detail in template_settings.iteritems(): list = [details['seriesid'], main_series_termid, '0'] while details[detail] == '': try: details[detail] = templates_per_series[list.pop( 0)][template_setting] except KeyError: continue publishtime = gettime(details['time']) if publishtime: local_datetime = timezone.localize( datetime.combine(entry, publishtime.time())) details['post_date_gmt'] = local_datetime.astimezone(pytz.utc) newpost = WordPressPost() newpost.post_type = 'podcast' newpost.title = details['title'] newpost.date = details['post_date_gmt'] newpost.post_status = 'draft' newpost.content = details['content'] newpost.terms = [wp.call(GetTerm('series', main_series_termid))] if details['seriesid']: newpost.terms.append( wp.call(GetTerm('series', details['seriesid']))) newpost.custom_fields = [{ 'key': 'preacher', 'value': details['preacher'] }, { 'key': 'date_recorded', 'value': details['date_recorded'] }, { 'key': 'bible_passage', 'value': details['bible_passage'] }, { 'key': 'episode_number', 'value': details['episode_number'] }, { 'key': 'publish_now', 'value': 'on' }] newpost.id = wp.call(NewPost(newpost)) if newpost.id: print('Created Post ID ' + str(newpost.id) + ' for date: ' + details['date_recorded'])
newtweet = newtweet + "<br>\n" + urls[i] + " " else: newtweet = newtweet + "<br>\n<a target=\"_blank\" href=\"" + urls[ i] + "\">" + urls[i] + "</a> " i += 1 continue #else add an image to the new tweet else: for mediaurl in mediaurls: newtweet = newtweet + mediaurl + " " continue #if we are still around, this is just a boring, normal word newtweet = newtweet + word + " " #let's compose our blogpost post = WordPressPost() #pick first 8 words for title post.title = ' '.join(TAG_RE.sub('', newtweet).split()[:8]) post.content = newtweet post.terms = tags post.date = datetime.strptime( tweet['created_at'].split()[0] + " " + tweet['created_at'].split()[1], '%Y-%m-%d %H:%M:%S') post.post_status = 'publish' post.id = client.call(posts.NewPost(post)) #print (newtweet) except KeyboardInterrupt: print("") print("Interrupted! Come back any time.") sys.exit()