Ejemplo n.º 1
0
def newJSON():
    # Get the authorization token from a file (Or from any other source.
    #   We just need a string containing the authorization token.
    authToken = api.getAuthTokenFromFile('token.txt')

    # Create our API object, giving the name of the server running the
    #   instance of Canvas and the authorization token that we'll use to
    #   authenticate our requests.
    apiObj = api.CanvasAPI('canvas.sfu.ca', authToken)

    # Call the API and get the all the discussion topics.
    topics = apiObj.allPages('courses/15003/discussion_topics')
    
    # run a for loop to extract entries
    for x in range(0, len(topics)):
        # prepare a new JSON document
        jayson = {}
        # store the topic id first
        jayson['discussion_num'] = topics[x]['id']
        jayson['discussion_title'] = topics[x]['title']
        # then get the messages
        results = apiObj.allPages('courses/15003/discussion_topics/' + str(topics[x]['id']) + '/view')[0]['view']
        # store the message in json post
        j = json.loads(json.dumps(results))
        jayson['posts'] = j
        # remember the list of topic
        if topics[x]['id'] not in t:
            t.insert(len(t)+1, topics[x]['id'])
        # store the jayson to mongoDB
        RAW_DATA.save(jayson)
    print("done storing")
Ejemplo n.º 2
0
def call_api_csv(tokenFilePath, server, apiEndpoint):
    """\
Call allPages with the appropriate parameters to get all results from the requested API call.  Convert these results to CSV format.  Return a string
"""

    token = api.getAuthTokenFromFile(tokenFilePath)
    apiObj = api.CanvasAPI(defaultServer=server, defaultAuthToken=token)
    
    result = apiObj.allPages(apiEndpoint)

    # Go through each dict object and look for arrays and dicts.
    #   These are complex data types that are not really compatible with CSV
    #   output format.  Change these types back into JSON strings so that if
    #   they contain data the user is interested in, they can process the JSON
    #   after the fact.
    for d in result:
        for k in d.keys():
            if type(d[k]) == dict or type(d[k]) == collections.OrderedDict or type(d[k]) == list:
                # Create an in-memory file for json.dump to work with.
                buf = io.StringIO()

                # Convert the object to a JSON string.
                json.dump(d[k], buf)

                # Replace the original object with the JSON string.
                d[k] = buf.getvalue()

    if len(result) > 0:
        # allKeys is acting simply as an ordered set here.  Not all
        #   of the results have the same set of data, which means that
        #   we need to look at the headers for ALL of the results instead
        #   of only the first one.
        allKeys = collections.OrderedDict()
        for r in result:
            ks = list(r.keys())
            for k in ks:
                allKeys[k] = 1

        # The list of headers in the order that we first saw them.
        headers = list(allKeys.keys())
    else:
        headers = []

    # csv.DictWriter requires a file to write to.  Use an in-memory file.
    buf = io.StringIO()

    # Write out the objects in CSV format.
    dw = csv.DictWriter(buf, headers)
    dw.writeheader()
    dw.writerows(result)

    # Return the CSV data as a string
    result = buf.getvalue()
    buf.close()
    return result
Ejemplo n.º 3
0
Archivo: emails.py Proyecto: cs10/cs10
#! /usr/bin/env python3

michael_path = '/Volumes/Macintosh HD/Users/Michael/Dropbox/Projects/'
import sys
sys.path.append(michael_path + 'canvaslms/canvaslms')

import canvaslms.api as api

# Get the authorization token from a file (Or from any other source.
#   We just need a string containing the authorization token.
authToken = api.getAuthTokenFromFile(michael_path + 'canvas_token.txt')
# Create our API object, giving the name of the server running the
#   instance of Canvas and the authorization token that we'll use to
#   authenticate our requests.
apiObj = api.CanvasAPI('https://bcourses.berkeley.edu', authToken)

stuemails = []
staffemails = []

base = 'https://bcourses.berkeley.edu/api/v1/'
cs10base = base + 'courses/1246916/'
url = cs10base + 'users?&include[]=email&per_page=550&enrollment_type='

cs10stu = apiObj.allPages(url + 'student', absoluteUrl=True, verbose=True)
cs10staff = apiObj.allPages(url + 'ta', absoluteUrl=True, verbose=True)

for stu in cs10stu:
    usremail = stu['email']
    stuemails.append(usremail)

for staff in cs10staff:
Ejemplo n.º 4
0
import canvaslms.api as api
import os
import urllib.request
authToken = api.getAuthTokenFromFile('./KEY')
apiObj = api.CanvasAPI('someschool.instructure.com', authToken) #Define url as school.instructure.com
results = apiObj.allPages('courses/')
for i in results:
	directory = i['course_code'].replace(" ","")
	if not os.path.exists(directory):
		os.makedirs(directory)
		classid = i['id']
		if classid == 14262: # Define classes that do not have any files. It will give urllib errors when it hits the class if not
			continue
		if classid == 165941: # More class defines
			continue
		if classid == 56779: #...more class defines
			continue
		print('For class: ' + str(classid))
		classfiledir = apiObj.allPages('courses/'+str(classid)+'/files')
		for j in classfiledir:
			if(os.path.exists(str(directory + '/' + j['filename'].replace("+","")))):
					print('Already have: ' + str(j['filename']) + ' for ' + str(i['course_code'].replace(" ","")))
			else:
					print('Grabbing : ' + str(j['filename']) + ' for ' + str(i['course_code'].replace(" ","")))
					urllib.request.urlretrieve(str(j['url']),str(directory + '/' + j['filename'].replace("+","")))
print('Done.')

Ejemplo n.º 5
0
import urllib.request
from canvaslms import api

# Load the oauth token
authToken = api.getAuthTokenFromFile("oauth.txt")

# Create the API object targeting the Evergreen Instructure instance
apiObj = api.CanvasAPI('evergreen.instructure.com', authToken)

values={
	"recipients[]":"744",
	"body":"I hope I get a different ID than 1745",
	"subject":"NEW SUBJECT",
	"group_conversation":"false"
}
data = urllib.parse.urlencode(values)
binary_data = data.encode('ascii')
results = apiObj.allPages('conversations', args=binary_data)

#results = apiObj.allPages('conversations/:1754/add_message', args=binary_data)

print(len(results))

# Ordered dictionary
response_dict = results[0]

for (key,value) in response_dict.items():
	print(str(key) + ":" + str(value))
Ejemplo n.º 6
0
def main():
    """\
    This script makes the specified call to the Canvas API, returning the result
    to standard output in CSV format.  The main function for this module allows
    it to be run from the command line with the following invocation:
    
        python3 -m canvaslms.bin.callapi -T TOKENFILE -H HOST APICALL

    where TOKENFILE, HOST, AND APICALL are the following:
     - TOKENFILE: path to the file containing the authentication token
       to be used for communicating with the API.
        - "/home/superdevel/some_project/tokenfile"
     - HOST: name of the server running Canvas
        - "canvas.instructure.com"
     - APICALL: the actuall call to make to the API
        - "courses/123456/users?enrollment_type=student"

    Of course, if an appropriate configuration file has been provided containing
    the authentication token file path and the Canvas host name, you can run the
    script simply as:

        python3 -m canvaslms.bin.callapi APICALL
    """

    # 0. Start out with an empty configuration dict
    config = {}

    # 1. Check CANVASLMS_API_CONFIG_PATH environment variable
    # If found, try to read a configuration dict from the path given.
    config_path = os.getenv("CANVASLMS_API_CONFIG_PATH")
    if config_path != None:
        # TODO: Instead of just dying here, print a warning to
        # standard error if the specified file doesn't exist.
        config = configutil.updatedict(config, configutil.readconfigfile(config_path))

    # 2. Check for CANVASLMS_API_CONFIG file in the current working directory
    if os.path.isfile("CANVASLMS_API_CONFIG"):
        config = configutil.updatedict(config, configutil.readconfigfile("CANVASLMS_API_CONFIG"))

    # Parse command line options
    parser = optparse.OptionParser()
    parser.add_option("-C", "--config-path", dest="ConfigPath")
    parser.add_option("-H", "--host", dest="CanvasHost")
    parser.add_option("-T", "--token-path", dest="TokenPath")
    parser.add_option('-A', "--get-auth-token", dest='GetAuthToken', default=False, action='store_true')
    parser.add_option('-V', '--csv-output', dest='CSVOutput', default=False, action='store_true')
    (options, args) = parser.parse_args()

    # 3. Check for a --config argument
    if options.ConfigPath != None:
        # TODO: We do actually want to die here.  If the user
        # explicitly specified a path to a config file and the file
        # doesn't exist, we should throw an exception.
        config = configutil.updatedict(config, configutil.readconfigfile(options.ConfigPath))

    # 4. Update config with command line options and arguments
    config = configutil.updatedict(config, options.__dict__)
    if len(args) == 1:
        config['APICall'] = args[0]

    # Get the required configuration values from the configuration object
    tokenFilePath = configutil.get_required_config_value(config, 'TokenPath')
    server = configutil.get_required_config_value(config, 'CanvasHost')
    apiCall = configutil.get_required_config_value(config, 'APICall')

    # Get our auth token, either from the terminal or from a file.
    if options.GetAuthToken == True:
        token = getpass.getpass('Auth Token: ')
    else:
        token = api.getAuthTokenFromFile(tokenFilePath)

    # Make the call to the API and print the results to stdout.
    # Default output format is JSON (same as the API), but CSV output
    # is available with the -V command line option.
    apiObj = api.CanvasAPI(defaultServer=server, defaultAuthToken=token)
    results = apiObj.allPages(apiCall)
    if options.CSVOutput == True:
        print(apiutil.toCSVString(results))
    else:
        print(json.dumps(results, indent=4, separators=(',', ': ')))