Пример #1
0
def run(query):
    api = orb_api()
    api.base_url = 'http://localhost:8000'
    api.user_name = 'demo'
    api.api_key = '39b4043c69b8db27ddba761ba82479d00c8ccbb1'
    results = api.search(query)
    for result in results['objects']:
        print result['title']
Пример #2
0
def run():
    api = orb_api()
    api.base_url = 'http://localhost:8000'
    api.user_name = 'demo'
    api.api_key = '39b4043c69b8db27ddba761ba82479d00c8ccbb1'

    resource = orb_resource()
    resource.title = "Alex Test9311"
    resource.description = "something else to test with"

    id = api.add_resource(resource)
    if id:
        api.add_resource_image(id, '/home/alex/temp/image.jpg')
Пример #3
0
def run():
    api = orb_api()
    api.base_url = 'http://localhost:8000'
    api.user_name = 'demo'
    api.api_key = '39b4043c69b8db27ddba761ba82479d00c8ccbb1'

    resource = orb_resource()
    resource.title = "Alex Test9311"
    resource.description = "something else to test with"

    id = api.add_resource(resource)
    if id:
        api.add_resource_image(id,'/home/alex/temp/image.jpg')
Пример #4
0
def run(orb_url, orb_username, orb_key):
    api = orb_api()
    api.base_url = orb_url
    api.user_name = orb_username
    api.api_key = orb_key
    api.verbose_output = DEBUG

    with open(INFILE, 'rb') as csvfile:
        file_reader = csv.reader(csvfile, delimiter=',', quotechar='"')
        for counter, row in enumerate(file_reader):
            # skip first row as has the headings
            if counter == 0:
                continue

            # skip if no title
            if row[CSV_FORMAT['title']].strip() == "":
                continue

            resource = orb_resource()
            resource.title = row[CSV_FORMAT['title']]

            # get the video id
            youtube_link = row[CSV_FORMAT['youtube_link']].split('/')
            video_id = youtube_link[len(youtube_link) - 1]

            resource.description = row[CSV_FORMAT['description']].decode('utf-8') + '<div style="text-align:center;">' +\
                         '<iframe width="560" height="315" src="https://www.youtube.com/embed/'+\
                          video_id +\
                          '?rel=0&html5=1" frameborder="0" allowfullscreen></iframe></div>'

            if row[CSV_FORMAT['study_time']] != '':
                resource.study_time_number = row[CSV_FORMAT['study_time']]
                resource.study_time_unit = 'mins'

            try:
                resource.id = api.add_resource(resource)
            except ORBAPIResourceExistsException, e:
                if DEBUG:
                    print e.message + ", id no:" + str(e.pk)
                resource.id = e.pk
                api.update_resource(resource)

            # get the resource id
            resource_from_api = api.get_resource(resource)

            # remove all ResourceFiles
            api.delete_resource_files(resource_from_api['files'])

            # remove all ResourceURLs
            api.delete_resource_urls(resource_from_api['urls'])

            # remove all tags for resource
            api.delete_resource_tags(resource_from_api['tags'])

            # get resource image from YouTube
            image_file_path = os.path.join('/tmp', video_id + '.jpg')
            if not os.path.exists(image_file_path):
                urllib.urlretrieve(
                    "http://img.youtube.com/vi/%s/mqdefault.jpg" % (video_id),
                    image_file_path)

            api.add_or_update_resource_image(resource.id, image_file_path)

            # add all the default tags
            for tag in MPOWERING_DEFAULT_TAGS:
                api.add_resource_tag(resource.id, tag.strip())

            # add resource specific tags
            specific_tags = row[CSV_FORMAT['health-domain']] + "," + row[
                CSV_FORMAT['audience']] + "," + row[CSV_FORMAT['geography']]
            tag_list = [x.strip() for x in specific_tags.split(',')]
            for tag in tag_list:
                api.add_resource_tag(resource.id, tag.strip())

            for lang in LANGUAGES:
                if row[CSV_FORMAT[lang]].strip() != "":
                    if DEBUG:
                        print "adding url: " + row[CSV_FORMAT[lang]]
                    resource_url = orb_resource_url()
                    resource_url.title = "View/Download (" + lang + ")"
                    resource_url.url = row[CSV_FORMAT[lang]]

                    api.add_resource_url(resource.id, resource_url)
                    api.add_resource_tag(resource.id, lang.strip())
Пример #5
0
def run(orb_url, orb_username, orb_key):
    api = orb_api()
    api.base_url = orb_url
    api.user_name = orb_username
    api.api_key = orb_key
    api.verbose_output = DEBUG

    with open(INFILE, 'rb') as csvfile:
        file_reader = csv.reader(csvfile, delimiter=',', quotechar='"')
        for counter, row in enumerate(file_reader):
            # skip first row as has the headings
            if counter == 0:
                continue

            # skip if no title
            if row[CSV_FORMAT['title']].strip() == "":
                continue

            resource = orb_resource()
            resource.title =  row[CSV_FORMAT['title']]

            # get the video info from Vimeo
            req = urllib2.Request("https://vimeo.com/api/oembed.json?maxwidth=500&url=" + row[CSV_FORMAT['preview']],
                                  headers={ 'User-Agent': 'Mozilla/5.0',
                                           })
            response = urllib2.urlopen(req)
            if response.code == HTML_OK:
                vimeo_data = json.loads(response.read())
            else:
                if DEBUG:
                    print "Error connecting to Vimeo server"
                continue

            resource.description = row[CSV_FORMAT['description']].decode('utf-8') + '<div style="text-align:center;">' + vimeo_data['html'].decode('utf-8') + '</div>'

            if row[CSV_FORMAT['study_time']] != '':
                resource.study_time_number = row[CSV_FORMAT['study_time']]
                resource.study_time_unit = 'mins'

            try:
                resource.id = api.add_resource(resource)
            except ORBAPIResourceExistsException, e:
                if DEBUG:
                    print e.message + ", id no:" + str(e.pk)
                resource.id = e.pk
                api.update_resource(resource)


            # get the resource id
            resource_from_api = api.get_resource(resource)

            # remove all ResourceFiles
            api.delete_resource_files(resource_from_api['files'])

            # remove all ResourceURLs
            api.delete_resource_urls(resource_from_api['urls'])

            # remove all tags for resource
            api.delete_resource_tags(resource_from_api['tags'])

            # get resource image from vimeo
            image_file_path = os.path.join('/tmp', str(vimeo_data['video_id']) + '.jpg')
            urllib.urlretrieve (vimeo_data['thumbnail_url'], image_file_path )

            api.add_or_update_resource_image(resource.id, image_file_path)

            # add all the default tags
            for tag in MPOWERING_DEFAULT_TAGS:
                api.add_resource_tag(resource.id, tag.strip())

            # add resource specific tags
            specific_tags = row[CSV_FORMAT['health-domain']] + "," + row[CSV_FORMAT['audience']] + "," + row[CSV_FORMAT['language']] + "," + row[CSV_FORMAT['geography']]
            tag_list = [x.strip() for x in specific_tags.split(',')]
            for tag in tag_list:
                api.add_resource_tag(resource.id, tag.strip())


            # add the urls/downloads
            if row[CSV_FORMAT['preview']].strip() != "":
                if DEBUG:
                    print "adding url: " + row[CSV_FORMAT['preview']]
                resource_url = orb_resource_url()
                resource_url.title = "View/Download on Vimeo (" + row[CSV_FORMAT['language']] + ")"
                resource_url.url = row[CSV_FORMAT['preview']]

                api.add_resource_url(resource.id,resource_url)

            other_langs_list = ['French',
                                'Swahili',
                                'Somali',
                                'Amharic',
                                'Portugese',
                                'Dari',
                                'Bemba',
                                'Luganda',
                                'Yoruba',
                                'Hausa',
                                'Saint Lucian Creole',
                                'Khmer',
                                'Burmese',
                                'Vietnamese',
                                'Indonesian',
                                'Kinyarwanda',]
            for ol in other_langs_list:
                if row[CSV_FORMAT[ol]].strip() != "":
                    if DEBUG:
                        print "adding url: " + row[CSV_FORMAT[ol]]
                    resource_url = orb_resource_url()
                    resource_url.title = "View/Download on Vimeo ("+ ol +")"
                    resource_url.url = row[CSV_FORMAT[ol]]

                    api.add_resource_url(resource.id, resource_url)
                    api.add_resource_tag(resource.id, ol.strip())
Пример #6
0
def run(orb_url, orb_username, orb_key):
    api = orb_api()
    api.base_url = orb_url
    api.user_name = orb_username
    api.api_key = orb_key
    api.verbose_output = DEBUG

    with open(INFILE, 'rb') as csvfile:
        file_reader = csv.reader(csvfile, delimiter=',', quotechar='"')
        for counter, row in enumerate(file_reader):
            # skip first row as has the headings
            if counter == 0:
                continue

            # skip if no title
            if row[CSV_FORMAT['title']].strip() == "":
                continue

            resource = orb_resource()
            resource.title =  row[CSV_FORMAT['title']]
            
            # get the video id 
            youtube_link = None
            LANGUAGES = []
            if row[CSV_FORMAT['English']].strip() != "":
                youtube_link = row[CSV_FORMAT['English']].strip()
                LANGUAGES.append("English")
            if row[CSV_FORMAT['Spanish']].strip() != "":
                youtube_link = row[CSV_FORMAT['Spanish']].strip()
                LANGUAGES.append("Spanish")
            if row[CSV_FORMAT['Portuguese']].strip() != "":
                youtube_link = row[CSV_FORMAT['Portuguese']].strip()
                LANGUAGES.append("Portuguese")
                
            if youtube_link: 
                
                youtube_link = youtube_link.split('/')
                video_id = youtube_link[len(youtube_link)-1]
                
                resource.description = row[CSV_FORMAT['description']].decode('utf-8') + '<div style="text-align:center;">' +\
                             '<iframe width="560" height="315" src="https://www.youtube.com/embed/'+\
                              video_id +\
                              '?rel=0&html5=1" frameborder="0" allowfullscreen></iframe></div>'
                              
                # get resource image from YouTube             
                image_file_path = os.path.join('/tmp', video_id + '.jpg')
                if not os.path.exists(image_file_path):
                    urllib.urlretrieve ("http://img.youtube.com/vi/%s/mqdefault.jpg" % (video_id), image_file_path )
            else:
                image_file_path = None
                resource.description = row[CSV_FORMAT['description']].decode('utf-8') 
                
            try:
                resource.id = api.add_resource(resource)
            except ORBAPIResourceExistsException, e:
                if DEBUG:
                    print e.message + ", id no:" + str(e.pk)
                resource.id = e.pk
                api.update_resource(resource)

            # get the resource id
            resource_from_api = api.get_resource(resource)

            # remove all ResourceFiles
            api.delete_resource_files(resource_from_api['files'])

            # remove all ResourceURLs
            api.delete_resource_urls(resource_from_api['urls'])

            # remove all tags for resource
            api.delete_resource_tags(resource_from_api['tags'])
                
            if image_file_path:
                api.add_or_update_resource_image(resource.id, image_file_path)
            
                
            # add resource specific tags
            specific_tags = row[CSV_FORMAT['license']] + "," + row[CSV_FORMAT['resource-type']] + "," + row[CSV_FORMAT['organisation']] + "," + row[CSV_FORMAT['health-domain']] + "," + row[CSV_FORMAT['audience']] + "," + row[CSV_FORMAT['geography']]
            tag_list = [x.strip() for x in specific_tags.split(',')]
            for tag in tag_list:
                api.add_resource_tag(resource.id, tag.strip())

            for lang in LANGUAGES:
                if row[CSV_FORMAT[lang]].strip() != "":
                    if DEBUG:
                        print "adding url: " + row[CSV_FORMAT[lang]]
                    resource_url = orb_resource_url()
                    resource_url.title = "View/Download ("+ lang +")"
                    resource_url.url = row[CSV_FORMAT[lang]]

                    api.add_resource_url(resource.id, resource_url)
                    api.add_resource_tag(resource.id, lang.strip())
Пример #7
0
def run(orb_url, orb_username, orb_key, youtube_key):
    api = orb_api()
    api.base_url = orb_url
    api.user_name = orb_username
    api.api_key = orb_key
    api.verbose_output = DEBUG

    with codecs.open(INFILE, 'rb', 'utf-8') as csvfile:
        file_reader = csv.reader(csvfile, delimiter=',', quotechar='"')
        for counter, row in enumerate(file_reader):
            # skip first row as has the headings
            if counter == 0:
                continue

            # skip if no title
            if row[CSV_FORMAT['title']].strip() == "":
                continue

            resource = orb_resource()
            resource.title = row[CSV_FORMAT['title']]

            # get the video id
            youtube_link = row[CSV_FORMAT['youtube_link']].split('=')
            video_id = youtube_link[len(youtube_link) - 1]

            if not video_id:
                continue
            youtube_url = str(
                "https://www.googleapis.com/youtube/v3/videos?id={video_id}&key={youtube_key}&part=snippet,contentDetails,statistics,status"
            ).format(video_id=video_id, youtube_key=youtube_key)
            req = urllib2.Request(youtube_url,
                                  headers={
                                      'User-Agent': 'Mozilla/5.0',
                                  })

            response = urllib2.urlopen(req)
            video_data = json.loads(response.read())

            if row[CSV_FORMAT['description']].encode('utf-8').strip():
                description = row[CSV_FORMAT['description']]
            else:
                description = video_data['items'][0]['snippet'][
                    'description'].replace(
                        "For more information and related videos visit us on http://www.digitalgreen.org/",
                        "").replace('"', "")

            if row[CSV_FORMAT['description']].strip() == "":
                continue

            resource.description = description + '<div style="text-align:center;">' +\
                         '<iframe width="560" height="315" src="https://www.youtube.com/embed/'+\
                          video_id +\
                          '?rel=0&html5=1" frameborder="0" allowfullscreen></iframe></div>'

            if row[CSV_FORMAT['study_time']] != '':
                resource.study_time_number = row[CSV_FORMAT['study_time']]
                resource.study_time_unit = 'mins'

            try:
                resource.id = api.add_resource(resource)
            except ORBAPIResourceExistsException, e:
                if DEBUG:
                    print e.message + ", id no:" + str(e.pk)
                resource.id = e.pk
                api.update_resource(resource)

            # get the resource id
            resource_from_api = api.get_resource(resource)

            # remove all ResourceFiles
            api.delete_resource_files(resource_from_api['files'])

            # remove all ResourceURLs
            api.delete_resource_urls(resource_from_api['urls'])

            # remove all tags for resource
            api.delete_resource_tags(resource_from_api['tags'])

            # get resource image from YouTube
            image_file_path = os.path.join('/tmp', video_id + '.jpg')
            if not os.path.exists(image_file_path):
                urllib.urlretrieve(
                    "http://img.youtube.com/vi/%s/mqdefault.jpg" % (video_id),
                    image_file_path)

            api.add_or_update_resource_image(resource.id, image_file_path)

            # add all the default tags
            for tag in MPOWERING_DEFAULT_TAGS:
                api.add_resource_tag(resource.id, tag.strip())

            # add resource specific tags
            specific_tags = row[CSV_FORMAT['health_domain']] + "," + row[
                CSV_FORMAT['geography']]
            tag_list = [x.strip() for x in specific_tags.split(',')]
            for tag in tag_list:
                api.add_resource_tag(resource.id, tag.strip())

            language = row[CSV_FORMAT['language']].strip()
            if language != "":
                if DEBUG:
                    print "adding url: " + row[CSV_FORMAT['youtube_link']]
                resource_url = orb_resource_url()
                resource_url.title = "View on YouTube (" + language + ")"
                resource_url.url = row[CSV_FORMAT['youtube_link']]

                api.add_resource_url(resource.id, resource_url)
                api.add_resource_tag(resource.id, language)

                # add link to Digital Green website
                resource_url = orb_resource_url()
                resource_url.title = "View on Digital Green website"
                resource_url.url = row[CSV_FORMAT['dg_weblink']]

                api.add_resource_url(resource.id, resource_url)
def run(orb_url, orb_username, orb_key, youtube_key):
    api = orb_api()
    api.base_url = orb_url
    api.user_name = orb_username
    api.api_key = orb_key
    api.verbose_output = DEBUG

    with codecs.open(INFILE, 'rb', 'utf-8') as csvfile:
        file_reader = csv.reader(csvfile, delimiter=',', quotechar='"')
        for counter, row in enumerate(file_reader):
            # skip first row as has the headings
            if counter == 0:
                continue

            # skip if no title
            if row[CSV_FORMAT['title']].strip() == "":
                continue

            resource = orb_resource()
            resource.title =  row[CSV_FORMAT['title']]

            # get the video id
            youtube_link = row[CSV_FORMAT['youtube_link']].split('=')
            video_id = youtube_link[len(youtube_link)-1]

            if not video_id:
                continue
            youtube_url = str("https://www.googleapis.com/youtube/v3/videos?id={video_id}&key={youtube_key}&part=snippet,contentDetails,statistics,status").format(video_id=video_id,youtube_key=youtube_key)
            req = urllib2.Request(youtube_url,
                                  headers={ 'User-Agent': 'Mozilla/5.0',
                                           })

            response = urllib2.urlopen(req)
            video_data = json.loads(response.read())

            if row[CSV_FORMAT['description']].encode('utf-8').strip():
                description  = row[CSV_FORMAT['description']]
            else:
                description = video_data['items'][0]['snippet']['description'].replace("For more information and related videos visit us on http://www.digitalgreen.org/", "").replace('"',"")

            if row[CSV_FORMAT['description']].strip() == "":
                continue

            resource.description = description + '<div style="text-align:center;">' +\
                         '<iframe width="560" height="315" src="https://www.youtube.com/embed/'+\
                          video_id +\
                          '?rel=0&html5=1" frameborder="0" allowfullscreen></iframe></div>'

            if row[CSV_FORMAT['study_time']] != '':
                resource.study_time_number = row[CSV_FORMAT['study_time']]
                resource.study_time_unit = 'mins'

            try:
                resource.id = api.add_resource(resource)
            except ORBAPIResourceExistsException, e:
                if DEBUG:
                    print e.message + ", id no:" + str(e.pk)
                resource.id = e.pk
                api.update_resource(resource)


            # get the resource id
            resource_from_api = api.get_resource(resource)

            # remove all ResourceFiles
            api.delete_resource_files(resource_from_api['files'])

            # remove all ResourceURLs
            api.delete_resource_urls(resource_from_api['urls'])

            # remove all tags for resource
            api.delete_resource_tags(resource_from_api['tags'])

            # get resource image from YouTube
            image_file_path = os.path.join('/tmp', video_id + '.jpg')
            if not os.path.exists(image_file_path):
                urllib.urlretrieve ("http://img.youtube.com/vi/%s/mqdefault.jpg" % (video_id), image_file_path )

            api.add_or_update_resource_image(resource.id, image_file_path)

            # add all the default tags
            for tag in MPOWERING_DEFAULT_TAGS:
                api.add_resource_tag(resource.id, tag.strip())

            # add resource specific tags
            specific_tags = row[CSV_FORMAT['health_domain']] + ","  + row[CSV_FORMAT['geography']]
            tag_list = [x.strip() for x in specific_tags.split(',')]
            for tag in tag_list:
                api.add_resource_tag(resource.id, tag.strip())

            language = row[CSV_FORMAT['language']].strip()
            if language != "":
                if DEBUG:
                    print "adding url: " + row[CSV_FORMAT['youtube_link']]
                resource_url = orb_resource_url()
                resource_url.title = "View on YouTube ("+ language +")"
                resource_url.url = row[CSV_FORMAT['youtube_link']]

                api.add_resource_url(resource.id, resource_url)
                api.add_resource_tag(resource.id, language)

                # add link to Digital Green website
                resource_url = orb_resource_url()
                resource_url.title = "View on Digital Green website"
                resource_url.url = row[CSV_FORMAT['dg_weblink']]

                api.add_resource_url(resource.id, resource_url)
Пример #9
0
def run(orb_url, orb_username, orb_key, db_name, db_user, db_passwd,
        update_files):
    api = orb_api()
    api.base_url = orb_url
    api.user_name = orb_username
    api.api_key = orb_key
    api.verbose_output = DEBUG

    db = MySQLdb.connect(host="localhost",
                         user=db_user,
                         passwd=db_passwd,
                         db=db_name)

    db.autocommit(True)

    # you must create a Cursor object. It will let
    #  you execute all the queries you need
    cur = db.cursor(MySQLdb.cursors.DictCursor)

    if update_files == 'True':
        '''
        Update with the most recent Moodle backup versions
        '''
        cur.execute(
            "SELECT f.id, course_id, f.description, file, moodle_course_id, type, short_name, c.location_code FROM dc_file f INNER JOIN dc_course c ON c.id = f.course_id"
        )
        for row in cur.fetchall():
            course_backups = glob.glob(MOODLE_BACKUP_DIR +
                                       'backup-moodle2-course-' +
                                       str(row['moodle_course_id']) + '-*.mbz')
            course_backups.sort()
            file_name = course_backups[len(course_backups) - 1]
            file_date = re.findall(r'[0-9]{8}', file_name)

            old_course_backups = glob.glob(
                os.path.join(OUTPUT_DIR_BASE, row['location_code'],
                             'moodle-backups',
                             row['short_name'] + '-' + row['type'] + '*.mbz'))
            for old_file in old_course_backups:
                if DEBUG:
                    print "Removing: " + old_file
                os.remove(old_file)

            new_file_name = row['short_name'] + "-" + row[
                'type'] + "-" + file_date[0] + ".mbz"
            if DEBUG:
                print "Copying over: " + new_file_name
            shutil.copy2(
                file_name,
                os.path.join(OUTPUT_DIR_BASE, row['location_code'],
                             'moodle-backups', new_file_name))

            #cur2 = db.cursor(MySQLdb.cursors.DictCursor)
            cur.execute("""UPDATE dc_file SET file = '%s' WHERE id = %s """ %
                        (new_file_name, int(row['id'])))
    '''
    Publish updates to the mPowering
    '''
    cur.execute(
        """SELECT id, title, description, icon, tags, location_code, study_hours FROM dc_course WHERE mpowering = 1 """
    )

    additional_desc = "<p>This course is part of the Ethiopia Federal Ministry of Health approved upgrade training program for Health Extension Workers.</p>"

    # print all the first cell of all the rows
    for row in cur.fetchall():

        resource = orb_resource()
        resource.title = row['title'].decode('utf-8')
        resource.description = row['description'].decode(
            'utf-8') + additional_desc
        if row['study_hours'] != None:
            resource.study_time_number = row['study_hours']
            resource.study_time_unit = 'hours'

        try:
            resource.id = api.add_resource(resource)
        except ORBAPIResourceExistsException, e:
            if DEBUG:
                print e.message + ", id no:" + str(e.pk)
            resource.id = e.pk
            # upate the resource
            api.update_resource(resource)

        if row['icon']:
            api.add_or_update_resource_image(
                resource.id, os.path.join(IMAGE_DIR_BASE, row['icon']))

        # get the resource id
        resource_from_api = api.get_resource(resource)

        # remove all ResourceFiles
        api.delete_resource_files(resource_from_api['files'])

        # remove all ResourceURLs
        api.delete_resource_urls(resource_from_api['urls'])

        # remove all tags for resource
        api.delete_resource_tags(resource_from_api['tags'])

        # add all the default tags
        for tag in MPOWERING_DEFAULT_TAGS:
            api.add_resource_tag(resource.id, tag.strip())

        # add resource specific tags
        if row['tags']:
            tag_list = [x.strip() for x in row['tags'].split(',')]
            for tag in tag_list:
                api.add_resource_tag(resource.id, tag.strip())

        # add the files
        cur.execute("""SELECT * FROM dc_file WHERE course_id = %s """ %
                    (row['id']))
        for file in cur.fetchall():
            resource_file = orb_resource_file()
            resource_file.title = file['title']
            resource_file.description = file['description']
            resource_file.order_by = file['orderby']
            resource_file.file = os.path.join(OUTPUT_DIR_BASE,
                                              row['location_code'],
                                              'moodle-backups', file['file'])
            api.add_resource_file(resource.id, resource_file)

        # add the urls
        cur.execute("""SELECT * FROM dc_url WHERE course_id = %s """ %
                    (row['id']))
        for url in cur.fetchall():
            resource_url = orb_resource_url()
            resource_url.title = url['title']
            resource_url.description = url['description']
            resource_url.order_by = url['orderby']
            resource_url.url = url['url']
            api.add_resource_url(resource.id, resource_url)
def run(orb_url, orb_username, orb_key):
    api = orb_api()
    api.base_url = orb_url
    api.user_name = orb_username
    api.api_key = orb_key
    api.verbose_output = DEBUG

    with open(INFILE, 'rb') as csvfile:
        file_reader = csv.reader(csvfile, delimiter=',', quotechar='"')
        for counter, row in enumerate(file_reader):
            # skip first row as has the headings
            if counter == 0:
                continue

            # skip if no title
            if row[CSV_FORMAT['title']].strip() == "":
                continue

            resource = orb_resource()
            resource.title = row[CSV_FORMAT['title']]

            # get the video info from Vimeo
            req = urllib2.Request(
                "https://vimeo.com/api/oembed.json?maxwidth=500&url=" +
                row[CSV_FORMAT['preview']],
                headers={
                    'User-Agent': 'Mozilla/5.0',
                })
            response = urllib2.urlopen(req)
            if response.code == HTML_OK:
                vimeo_data = json.loads(response.read())
            else:
                if DEBUG:
                    print "Error connecting to Vimeo server"
                continue

            resource.description = row[CSV_FORMAT['description']].decode(
                'utf-8') + '<div style="text-align:center;">' + vimeo_data[
                    'html'].decode('utf-8') + '</div>'

            if row[CSV_FORMAT['study_time']] != '':
                resource.study_time_number = row[CSV_FORMAT['study_time']]
                resource.study_time_unit = 'mins'

            try:
                resource.id = api.add_resource(resource)
            except ORBAPIResourceExistsException, e:
                if DEBUG:
                    print e.message + ", id no:" + str(e.pk)
                resource.id = e.pk
                api.update_resource(resource)

            # get the resource id
            resource_from_api = api.get_resource(resource)

            # remove all ResourceFiles
            api.delete_resource_files(resource_from_api['files'])

            # remove all ResourceURLs
            api.delete_resource_urls(resource_from_api['urls'])

            # remove all tags for resource
            api.delete_resource_tags(resource_from_api['tags'])

            # get resource image from vimeo
            image_file_path = os.path.join(
                '/tmp',
                str(vimeo_data['video_id']) + '.jpg')
            urllib.urlretrieve(vimeo_data['thumbnail_url'], image_file_path)

            api.add_or_update_resource_image(resource.id, image_file_path)

            # add all the default tags
            for tag in MPOWERING_DEFAULT_TAGS:
                api.add_resource_tag(resource.id, tag.strip())

            # add resource specific tags
            specific_tags = row[CSV_FORMAT['health-domain']] + "," + row[
                CSV_FORMAT['audience']] + "," + row[CSV_FORMAT[
                    'language']] + "," + row[CSV_FORMAT['geography']]
            tag_list = [x.strip() for x in specific_tags.split(',')]
            for tag in tag_list:
                api.add_resource_tag(resource.id, tag.strip())

            # add the urls/downloads
            if row[CSV_FORMAT['preview']].strip() != "":
                if DEBUG:
                    print "adding url: " + row[CSV_FORMAT['preview']]
                resource_url = orb_resource_url()
                resource_url.title = "View/Download on Vimeo (" + row[
                    CSV_FORMAT['language']] + ")"
                resource_url.url = row[CSV_FORMAT['preview']]

                api.add_resource_url(resource.id, resource_url)

            other_langs_list = [
                'French',
                'Swahili',
                'Somali',
                'Amharic',
                'Portugese',
                'Dari',
                'Bemba',
                'Luganda',
                'Yoruba',
                'Hausa',
                'Saint Lucian Creole',
                'Khmer',
                'Burmese',
                'Vietnamese',
                'Indonesian',
                'Kinyarwanda',
            ]
            for ol in other_langs_list:
                if row[CSV_FORMAT[ol]].strip() != "":
                    if DEBUG:
                        print "adding url: " + row[CSV_FORMAT[ol]]
                    resource_url = orb_resource_url()
                    resource_url.title = "View/Download on Vimeo (" + ol + ")"
                    resource_url.url = row[CSV_FORMAT[ol]]

                    api.add_resource_url(resource.id, resource_url)
                    api.add_resource_tag(resource.id, ol.strip())
def run(orb_url, orb_username, orb_key, db_name, db_user, db_passwd, update_files):
    api = orb_api()
    api.base_url = orb_url
    api.user_name = orb_username
    api.api_key = orb_key
    api.verbose_output = DEBUG

    db = MySQLdb.connect(host="localhost",
                         user=db_user,
                          passwd=db_passwd,
                          db=db_name)

    db.autocommit(True)

    # you must create a Cursor object. It will let
    #  you execute all the queries you need
    cur = db.cursor(MySQLdb.cursors.DictCursor)

    if update_files == 'True':
        '''
        Update with the most recent Moodle backup versions
        '''
        cur.execute("SELECT f.id, course_id, f.description, file, moodle_course_id, type, short_name, c.location_code FROM dc_file f INNER JOIN dc_course c ON c.id = f.course_id")
        for row in cur.fetchall():
            course_backups = glob.glob(MOODLE_BACKUP_DIR+'backup-moodle2-course-'+str(row['moodle_course_id'])+'-*.mbz')
            course_backups.sort()
            file_name = course_backups[len(course_backups)-1]
            file_date = re.findall(r'[0-9]{8}', file_name)

            old_course_backups = glob.glob(os.path.join(OUTPUT_DIR_BASE, row['location_code'], 'moodle-backups', row['short_name'] + '-' + row['type'] + '*.mbz'))
            for old_file in old_course_backups:
                if DEBUG:
                    print "Removing: " + old_file
                os.remove(old_file)

            new_file_name = row['short_name'] + "-" + row['type'] + "-" + file_date[0] +".mbz"
            if DEBUG:
                print "Copying over: " + new_file_name
            shutil.copy2(file_name, os.path.join(OUTPUT_DIR_BASE, row['location_code'], 'moodle-backups', new_file_name))

            #cur2 = db.cursor(MySQLdb.cursors.DictCursor)
            cur.execute("""UPDATE dc_file SET file = '%s' WHERE id = %s """ % (new_file_name, int(row['id'])))

    '''
    Publish updates to the mPowering
    '''
    cur.execute("""SELECT id, title, description, icon, tags, location_code, study_hours FROM dc_course WHERE mpowering = 1 """)

    additional_desc = "<p>This course is part of the Ethiopia Federal Ministry of Health approved upgrade training program for Health Extension Workers.</p>"

    # print all the first cell of all the rows
    for row in cur.fetchall() :

        resource = orb_resource()
        resource.title =  row['title'].decode('utf-8')
        resource.description = row['description'].decode('utf-8') + additional_desc
        if row['study_hours'] != None:
            resource.study_time_number = row['study_hours']
            resource.study_time_unit = 'hours'

        try:
            resource.id = api.add_resource(resource)
        except ORBAPIResourceExistsException, e:
            if DEBUG:
                print e.message + ", id no:" + str(e.pk)
            resource.id = e.pk
            # upate the resource
            api.update_resource(resource)

        if row['icon']:
            api.add_or_update_resource_image(resource.id, os.path.join(IMAGE_DIR_BASE, row['icon']))

        # get the resource id
        resource_from_api = api.get_resource(resource)

        # remove all ResourceFiles
        api.delete_resource_files(resource_from_api['files'])

        # remove all ResourceURLs
        api.delete_resource_urls(resource_from_api['urls'])

        # remove all tags for resource
        api.delete_resource_tags(resource_from_api['tags'])

        # add all the default tags
        for tag in MPOWERING_DEFAULT_TAGS:
            api.add_resource_tag(resource.id, tag.strip())

        # add resource specific tags
        if row['tags']:
            tag_list = [x.strip() for x in row['tags'].split(',')]
            for tag in tag_list:
                api.add_resource_tag(resource.id, tag.strip())

        # add the files
        cur.execute("""SELECT * FROM dc_file WHERE course_id = %s """ % (row['id']))
        for file in cur.fetchall():
            resource_file = orb_resource_file()
            resource_file.title = file['title']
            resource_file.description = file['description']
            resource_file.order_by = file['orderby']
            resource_file.file = os.path.join(OUTPUT_DIR_BASE, row['location_code'], 'moodle-backups', file['file'])
            api.add_resource_file(resource.id,resource_file)

        # add the urls
        cur.execute("""SELECT * FROM dc_url WHERE course_id = %s """ % (row['id']))
        for url in cur.fetchall():
            resource_url = orb_resource_url()
            resource_url.title = url['title']
            resource_url.description = url['description']
            resource_url.order_by = url['orderby']
            resource_url.url = url['url']
            api.add_resource_url(resource.id,resource_url)