def delete_lab(slug): ''' Deletes a lab specified by the slug parameter Args: slug: string specifying the slug of the lab ''' print('DELETING A SPECIFIC LAB') client = contentful_management.Client( labs_constants.CONTENTFUL_MANAGEMENT_TOKEN) entry = None try: print('Finding the lab %s...' % slug) entry = client.entries( labs_constants.CONTENTFUL_SPACE_ID, labs_constants.CONTENTFUL_ENVIRONMENT).find(slug) except contentful_management.errors.NotFoundError: print('Entry not found for slug %s, STOPPING delete step') return else: print('Found existing entry for slug %s, deleting...' % slug) entry.unpublish() entry.delete() print('FINISHED delete for slug %s' % slug)
def _get_contentful_client(): api_token = _get_config()['management_api_token'] client = getattr(sys.modules[__name__], '_contentful_client') if client is None: client = contentful_management.Client(api_token) setattr(sys.modules[__name__], '_contentful_client', client) return client
def __init__(self): ''' Must be called with a department already set. ''' self.client = contentful_management.Client(secure.MANAGEMENT_API_TOKEN) self.entries_client = self.client.entries(secure.SPACE_ID, secure.ENVIRONMENT_ID) self.content_types_client = self.client.content_types( secure.SPACE_ID, secure.ENVIRONMENT_ID) return None
def main(SPACE_ID, CF_ENV, PAT): cma = contentful_management.Client(PAT) content_types = cma.content_types(SPACE_ID, CF_ENV).all() if len(content_types.items) < 1: sys.exit('There\'s no content types in this space.') file_name = f'{SPACE_ID}_content_model_{datetime.date.today()}.xlsx' writer = pd.ExcelWriter(file_name) for content_type in content_types: ct = content_type.to_json() name = clean_name(ct['name']) fields = ct['fields'] df = pd.DataFrame(fields) df.to_excel(writer, name, index=False) writer.save()
def create_app(settings_override=None): app = factory.create_app(__name__, __path__, settings_override) contentful_delivery_client = contentful.Client( os.environ['CONTENTFUL_SPACE_ID'], os.environ['CONTENTFUL_DELIVERY_ACCESS_TOKEN']) contentful_management_client = contentful_management.Client( os.environ['CONTENTFUL_MANAGEMENT_ACCESS_TOKEN']) nltk.download('punkt') nltk.download('stopwords') @app.route('/v1') def abstract_docs(): return render_template('abstract.html') @app.route('/v1', methods=['POST']) def abstract(): if os.environ['FLASK_ENV'] == 'production': data = request.get_json() content_raw = data['fields']['content']['en-GB'] entry_id = data['sys']['id'] else: entry_id = 'jZwuzvJy0g4OICOQU4y2S' entry = contentful_delivery_client.entry(entry_id) content_raw = entry.content number_of_sents = 4 """ Determine contentful resource """ environment_id = 'master' entry = contentful_management_client.entries( os.environ['CONTENTFUL_SPACE_ID'], environment_id).find(entry_id) print('entry', entry) """ Remove markdown markup """ content_in_html = markdown(content_raw) content_soup = BeautifulSoup(content_in_html, features='html.parser') """ Remove code snippets while leaving inline code snippets in place """ [ s.extract() for s in content_soup('code') if len(s.prettify().splitlines()) > 3 ] """ Remove headings and subheadings """ headings = [s.extract() for s in content_soup('h1')] headings.extend([s.extract() for s in content_soup('h2')]) headings.extend([s.extract() for s in content_soup('h3')]) headings.extend([s.extract() for s in content_soup('h4')]) headings.extend([s.extract() for s in content_soup('h5')]) headings.extend([s.extract() for s in content_soup('h6')]) headings_in_text = " ".join([h.get_text() for h in headings]) """ Remove newline characters """ for s in content_soup.findAll(text=True): if '\n' in s.string and len(s.string) == 1: s.extract() """ Create bag of words """ content_in_text = content_soup.get_text() sents = sent_tokenize(content_in_text) if len(sents) < number_of_sents: return 'Not enough sentences for abstract extraction.', 403 words_in_content = word_tokenize(content_in_text.lower()) words_in_content.extend(word_tokenize(headings_in_text.lower())) _stopwords = set( stopwords.words('english') + list(punctuation) + ["''", "``"]) words_in_content = [ word for word in words_in_content if word not in _stopwords ] freq = FreqDist(words_in_content) top_10 = nlargest(10, freq, key=freq.get) """ Rank sentences by importance """ ranking = defaultdict(int) for i, sent in enumerate(sents): for w in word_tokenize(sent.lower()): if w in freq: ranking[i] += freq[w] """ Extract abstract """ sents_idx = nlargest(number_of_sents, ranking, key=ranking.get) abstract = " ".join([sents[j] for j in sorted(sents_idx)]) """ Write abstract to contentful """ if not os.environ['FLASK_ENV'] == 'development': entry_attributes = { 'content_type_id': 'post', 'fields': { **entry.fields(), **entry.fields_with_locales(), 'abstract': { 'en-GB': abstract } } } entry.update(entry_attributes) return abstract return app
def id_generate(): seed = random.getrandbits(32) while True: yield seed seed += 1 id_entry = id_generate() po = polib.pofile('main.po') for entry in po: client = contentful_management.Client( 'CFPAT-_QLbCxDyhpTnTR2FFvUSqrly5QCZxfHQzzNCDQdzroE') entry_id = next( id_entry) # Use `None` if you want the API to autogenerate the ID. entry = client.entries('orl9epj4q4ul', 'master').create( entry_id, { 'content_type_id': 'main', "fields": { "msgid": { "en-US": entry.msgid }, "msgstr": { "en-US": entry.msgstr } }
def __init__(self, api_token, space_id): client = contentful_management.Client(api_token) self.environment = client.environments(space_id).find('master')
import contentful_management import time import os from dotenv import load_dotenv load_dotenv() SPACE_ID = os.getenv("SPACE_ID") DELIVERY_API_KEY = os.getenv("DELIVERY_API_KEY") MANGEMENT_API_KEY = os.getenv("MANGEMENT_API_KEY") TESTING_ENV = "circle_testing" client = contentful_management.Client(MANGEMENT_API_KEY) try: environment = client.environments(SPACE_ID).find(TESTING_ENV) print("Deleting previous environment {}".format(TESTING_ENV)) environment.delete() except: print("{} environment doesn't exist".format(TESTING_ENV)) print("Creating new Enivronment: {}".format(TESTING_ENV)) environment = client.environments(SPACE_ID).create(TESTING_ENV, {"name": TESTING_ENV}) time.sleep(1) environment.name = TESTING_ENV print("Polling to detect creation of {}".format(TESTING_ENV)) while True: try:
#print(f"Arguments count: {len(sys.argv)}") if len(sys.argv) < 2: print('missing arguments for origin and destination space') quit() elif len(sys.argv) < 3: print('missing second argument for destination space') quit() # get spaces id from arguments orig_space = sys.argv[1] dest_space = sys.argv[2] cma_key = 'YOUR_CMA_KEY' #conect to cma client = contentful_management.Client(cma_key) # get all roles on dest space and add them to array my_input = [] dest_roles = client.roles(dest_space).all() for dest_role in dest_roles: dest_role_name = getattr(dest_role, 'name', 'Name not found') print(f"Role found on destination '{dest_space}': {dest_role_name}") my_input.append(dest_role_name) # on loop inserting roles, avoid already created roles roles = client.roles(orig_space).all() for role in roles: role_name = getattr(role, 'name', 'Not a product') if role_name not in my_input:
import contentful_management import re from pathlib import Path key_dir = Path(__file__).resolve().parent.parent.joinpath('KEYS') KEY = key_dir.joinpath('contentful.txt').read_text() # set up Contentful environment and get list of content types as starting point client = contentful_management.Client(KEY) space = client.spaces().all()[2].id ENV = client.environments(space).find('master') all_types = ENV.content_types().all() types = [t.id for t in all_types] #%% # Sublists of categorised content types pages = ['adviceCollection', 'adviceCollectionAdviser', 'adviceList'] units = [ 'adviceUnit', 'adviceUnitAdviser', 'callout', 'calloutAdviser', 'circumstantialUnit', 'circumstantialUnitAdviser' ] dynamic = [ 'banner', 'table', 'adviserWarning', 'amount', 'backgroundText', 'callCosts', 'contactDetails', 'contentPattern', 'legislationLink' ] tools = ['tool', 'resultPage', 'question', 'answer']
def _sys_field(self, cid): ''' param cid: contentful id ''' return {'sys': {'type': 'Link', 'linkType': 'Entry', 'id': cid}} def to_camel_case(self, string): components = string.replace('-', '_').split('_') return components[0] + ''.join(x.title() for x in components[1:]) if __name__ == "__main__": from pprint import pprint # Example using the Contentful client client = contentful_management.Client(secure.MANAGEMENT_API_TOKEN) sid = secure.SPACE_ID eid = secure.ENVIRONMENT_ID content_type = client.content_types(sid, eid).find('instructor') # Translation of metadata to a Contentful entry T = Translate() pprint( T.autoInstructor({ 'name': 'Daniel Seaton', 'title': 'Dr.', 'bio': 'New at ODL.', 'department_clink_id': '1c5BaHz1xsxiNogsMkMQPr' }))
from dotenv import load_dotenv import os import contentful import contentful_management load_dotenv() cc = contentful.Client(os.getenv("CONTENTFUL_SPACE_ID"), os.getenv("CONTENTFUL_ACCESS_TOKEN")) cm = contentful_management.Client(os.getenv("CONTENTFUL_PAT")) def write_guest(name, description=None): entry = cm.entries(os.getenv("CONTENTFUL_SPACE_ID"), os.getenv("CONTENTFUL_ENVIRONMENT")).create( None, { "content_type_id": "guest", "fields": { "name": { "en-US": name }, "description": { "en-US": description } } }) return entry def write_episode(title, number=None,
"id": id, "name": name, "slug": slug, "description": desc }) return product_types dotenv.load_dotenv() SPACE_ID = os.getenv("SPACE_ID") ACCESS_TOKEN = os.getenv("ACCESS_TOKEN_MGMT") ENVIRONMENT = "master" client = contentful_management.Client(ACCESS_TOKEN) content_type = client.content_types(SPACE_ID, ENVIRONMENT).find("productType") entries = content_type.entries().all({'limit': '1000'}) existing_product_types = [] for entry in entries: existing_product_types.append(str(entry.fields("de")["id"])) print( f"{len(existing_product_types)} product types found: {','.join(existing_product_types)}" ) product_types = load_product_types() for product_type in product_types: if product_type["id"] not in existing_product_types:
import json import os import re import shutil import labs_constants import boto3 from botocore.exceptions import NoCredentialsError import frontmatter import contentful_management client = contentful_management.Client( labs_constants.CONTENTFUL_MANAGEMENT_TOKEN) SLUG_BLACKLIST = [] def get_slugs(): ''' Gets the list of slugs, which are the directory names of each directory under the labs/ directory Returns: List of strings representing slugs ''' slugs = [] # r=root, d=directories, f = files for r, d, f in os.walk(labs_constants.LABS_PATH): slugs = [folder for folder in d]
def main(folder, drive_path): """A personal Photo Manager for Contentful and Google Drive. Creates a Root Google Drive folder, along with a year and individual folder for the album. Asks the user if they want to upload to Contentful as well, and if so asks for a collection Title. """ contentful_upload = promptUser( "Do you want to upload to Contentful? [Y/n]") drive_upload = promptUser("Do you want to upload to Google Drive? [Y/n]") if contentful_upload: collection_title = input( "Please provide a title for the Photo Collection:").strip() photos = [] collection_date = None print("===== Authenticating Contentful =====") try: client = contentful_management.Client(CONTENTFUL_MANAGEMENT_TOKEN) portfolio_space = client.spaces().find(CONTENTFUL_SPACE_ID) master_env = portfolio_space.environments().find('master') except: contentful_upload = False print( "!!!!! Authenticating Contentful Failed. Ensure the Management Token and Space ID are correct !!!!!" ) if drive_upload: service, folder_id = driveFolderSetup(folder) print("===== Iterating Through Photos =====") directory = os.fsencode(drive_path + DRIVE_YEAR + "/" + folder) for file in os.listdir(directory): filename = os.fsdecode(file) if filename.endswith(".jpg"): filepath = os.fsencode(drive_path + DRIVE_YEAR + "/" + folder + "/" + filename) if drive_upload: photo_metadata = {'name': [filename], 'parents': [folder_id]} photo_media = MediaFileUpload(filepath, mimetype='image/jpeg', resumable=True) request = service.files().create(media_body=photo_media, body=photo_metadata) response = None while response is None: status, response = request.next_chunk() if status: print("Uploaded", int(status.progress() * 100)) print("Uploaded", filename, "to Google Drive") if contentful_upload: if not collection_date: with open(os.fsdecode(filepath), 'rb') as image_file: image = Image(image_file) collection_date = datetime.datetime.strptime( image.datetime_original, '%Y:%m:%d %H:%M:%S').date() try: contentful_upload = portfolio_space.uploads().create( os.fsdecode(filepath)) asset_attributes = { 'fields': { 'title': { "en-US": filename }, 'file': { 'en-US': { 'fileName': filename, 'contentType': 'image/jpeg', 'uploadFrom': contentful_upload.to_link().to_json() } } } } contentful_asset = master_env.assets().create( None, asset_attributes) # None lets API generate ID contentful_asset = contentful_asset.process( ) # Processing is an async task, so I need to wait to publish it except: print('!!!!! Error Uploading Contentful Asset:', filename) else: photos.append(contentful_asset) print("Uploaded", filename, "to Contentful") if contentful_upload: print("===== Creating Contentful Photo Collection =====") photoCollection_attributes = { 'content_type_id': 'photoCollection', 'fields': { 'title': { 'en-US': collection_title }, 'photos': { 'en-US': [item.to_link().to_json() for item in photos] }, 'collectionDate': { 'en-US': collection_date.isoformat() }, 'featuredImage': { 'en-US': photos[0].to_link().to_json() } } } try: new_photoCollection = master_env.entries().create( None, photoCollection_attributes ) # None lets API auto generate unique ID except: print('!!!!! Error Creating Contentful Photo Collection !!!!!') print("===== Publishing Uploaded Contentful Images =====") for photo in photos: try: master_env.assets().find(photo.id).publish() except: print('!!!!! Error Publishing Contentful Asset:', photo.file.fileName) print("===== Contentful Upload and Publish Complete! =====")