def main(): if len(sys.argv) <= 1: print >> sys.stderr, "Usage: API.py <method> <accept> <path> [upload] [metadata]" exit() # user input method = sys.argv[1] accept = sys.argv[2] path = sys.argv[3] if len(sys.argv) > 4 : if 'http' not in sys.argv[4] : audioFilename = os.path.abspath(sys.argv[4]) # optional else : audioFilename = sys.argv[4] else: audioFilename = '' if len(sys.argv) > 5: metadataFilename = sys.argv[5] # optional else: metadataFilename = '' register_openers() request = make_request(method,accept, path, audioFilename, metadataFilename) # Actually do the request, and get the response try: response = urllib2.urlopen(request) except urllib2.HTTPError, e: print >> sys.stderr,"error" print >> sys.stderr,e print >> sys.stderr,e.read() return
def API_LoadLoggerData(LoggerFile, site_id, api, project, template_id): from encode import multipart_encode from streaminghttp import register_openers import urllib2, os, csv print LoggerFile a = open(LoggerFile, 'rb') logger = csv.reader(a) UpdateFile = 'Updater.csv' b = open(UpdateFile, 'rb') update = csv.reader(b) c = open('Upload_File.csv','wb') upload_data = csv.writer(c, delimiter=',',quoting=csv.QUOTE_NONE) data = list(logger) up = list(update) diff = len(data)-len(up) print "# New Data Rows=", diff if diff > 0: print "Creating Upload File" startline = 1 for row in data[-(diff):]: upload_data.writerow(row) a.close() b.close() c.close() upload_file = os.getcwd()+'\Upload_File.csv' print "File to Upload to VOEIS="+upload_file url='https://voeis.msu.montana.edu/projects/'+str(project)+'/apivs/upload_data.json?' # Register the streaming http handlers with urllib2 register_openers() # Start the multipart/form-data encoding of the file "DSC0001.jpg" # "image1" is the name of the parameter, which is normally set # via the "name" parameter of the HTML <input> tag. # headers contains the necessary Content-Type and Content-Length # datagen is a generator object that yields the encoded parameters datagen, headers = multipart_encode ({'datafile': open('Upload_File.csv', 'rb'), 'data_template_id': template_id, 'site_id': site_id, 'start_line': startline, 'api_key': api }) # Create the Request object request = urllib2.Request(url, datagen, headers) # Actually do the request, and get the response print urllib2.urlopen(request).read()
def PhotosUpload(self, photo_full_path, status="", source="", location=""): '''Upload img file via python 3rd-party lib ,poster.''' register_openers() img=open(photo_full_path,"rb") jsonData={ 'photo': img, 'status': status, 'source': source, 'location': location, } data,headers=multipart_encode(jsonData) api='photos_upload' url=str(Api(api)) return self._getJson(self.parent.xauth.apiUploadPhoto(url,data,headers))
def publish_screenshot(self, event): import time settings = get_settings() #t = time.strftime("%I%M%S") t = 'temp' print t time.sleep(1) screen = wx.ScreenDC() size = screen.GetSize() bmp = wx.EmptyBitmap(size[0], size[1]) mem = wx.MemoryDC(bmp) mem.Blit(0, 0, size[0], size[1], screen, 0, 0) del mem # Release bitmap bmp.SaveFile(t + '.png', wx.BITMAP_TYPE_PNG) opener = streaminghttp.register_openers() params = { 'image': open(t + '.png', 'rb'), 'lesson': settings['id'], 'publish': 'yes' } datagen, headers = multipart_encode(params) response = opener.open( urllib2.Request(settings['url2'], datagen, headers)) print 'Hello, world!'
def upload_music(music_file): register_openers() datagen, headers = multipart_encode({"files[]": open(music_file, "rb")}) headers[KEY_HOST] = HOST headers[KEY_CONNECTION] = CONNECTION headers[KEY_ACCEPT] = ACCEPT headers[KEY_USER_AGENT] = USER_AGENT headers[KEY_REFERER] = REFERER headers[KEY_ACCEPT_LANGUAGE] = ACCEPT_LANGUAGE headers[KEY_COOKIE] = COOKIE headers[KEY_ORIGIN] = ORIGIN headers[KEY_X_REQUESTED_WITH] = X_REQUESTED_WITH url = "https://chordify.net/song/file:" request = urllib2.Request(url, datagen, headers) print("POST: %s" % url) response = urllib2.urlopen(request).read() response_json = json.loads(response) print("response: %s" % response_json) if response_json.has_key("slug"): return (True, response_json["slug"]) else: return (False, "")
def downloadAdditionalFiles(url, target_path): try: opener = register_openers() params = {} datagen, headers = multipart_encode(params) request = urllib2.Request(url, datagen, headers) data = urllib2.urlopen(request).read() dom = parseString(data) for design in dom.getElementsByTagName("entry"): url = str(design.getAttributeNode("url").nodeValue) if not os.path.exists(target_path): os.makedirs(target_path) metrixTools.downloadFile(url, target_path + url.split("file=")[-1]) except: pass
def downloadAdditionalFiles(url, target_path): try: opener = register_openers() params = {} datagen, headers = multipart_encode(params) request = urllib2.Request(url, datagen, headers) data = urllib2.urlopen(request).read() dom = parseString(data) for design in dom.getElementsByTagName('entry'): url = str(design.getAttributeNode('url').nodeValue) if not os.path.exists(target_path): os.makedirs(target_path) metrixTools.downloadFile(url, target_path + url.split('file=')[-1]) except: pass
def getWeb(url, login = False, parameters = {}): try: opener = register_openers() if login and config.plugins.MetrixConnect.auth_token.value != 'None' and config.plugins.MetrixConnect.auth_token.value != '': params = {'auth_id': config.plugins.MetrixConnect.auth_id.value, 'device_id': getDeviceID(), 'auth_token': config.plugins.MetrixConnect.auth_token.value} params.update(parameters) else: params = parameters datagen, headers = multipart_encode(params) request = urllib2.Request(url, datagen, headers) data = urllib2.urlopen(request).read() return data except Exception as e: print '[MetrixCore] Error getting web: ' + url return False
def send_screenshot(self, event): import time settings = get_settings() #t = time.strftime("%I%M%S") t = 'temp' print t time.sleep(1) screen = wx.ScreenDC() size = screen.GetSize() bmp = wx.EmptyBitmap(size[0], size[1]) mem = wx.MemoryDC(bmp) mem.Blit(0, 0, size[0], size[1], screen, 0, 0) del mem # Release bitmap bmp.SaveFile(t+'.png', wx.BITMAP_TYPE_PNG) opener = streaminghttp.register_openers() params = {'image': open(t+'.png','rb'), 'topic': settings['id']} datagen, headers = encode.multipart_encode(params) response = opener.open(urllib2.Request(settings['url2'], datagen, headers)) print 'Hello, world!'
ni_digester.get_b64_encoded_length())} msgid=str(random.randint(1, 2**64)) param_list = [octet_param, ("URI", uri_dict), ("msgid", msgid), ("ext", ext), ("fullPut", "yes"), ("rform", rform)] # Construct data generator and header strings datagen, headers = multipart_encode(param_list) if verbose: debug("Parameters prepared: %s"% "".join(datagen)) # Set up streaming HTTP mechanism - register handlers with urllib2 # get out for now, don't do it opener = streaminghttp.register_openers() # Where to send the publish request. http_url = "http://%s/netinfproto/publish" % host # debug("Accessing: %s" % http_url) # Send POST request to destination server fsize=os.path.getsize(file_name) nilog("%s,PUBLISH tx,file,%s,size,%d,to,%s" % (msgid,file_name,fsize,host)) try: req = urllib2.Request(http_url, datagen, headers) except Exception, e: nilog("%s,PUBLISH tx error" % msgid); if verbose: nilog("Error: Unable to create request for http URL %s: %s" % (http_url, str(e))) f.close() return
#!/usr/bin/env python from encode import multipart_encode from streaminghttp import register_openers import urllib2,tkproductor,iteration,log register_openers() product_token = tkproductor.present_token upload_dic = {} upload_file = iteration.upload_file for upload_name in upload_file : upload_dic["key"] = upload_name upload_dic["token"] = product_token upload_dic["file"] = open(upload_name,"rb") log.writedown (upload_name) datagen, headers = multipart_encode(upload_dic) request = urllib2.Request("http://upload.qiniu.com/", datagen, headers) print urllib2.urlopen(request).read()
def main(argv=None): if argv is None: argv = sys.argv verbose = False uid = "" process_id = "" audioFilename = "" metadataFilename = "" accept = "text/xml" # NEED TO SPECIFIY USERNAME AND PASSWORD HERE username = "" password = "" opts, args = getopt.getopt( argv[1:], "vhi:p:u:m:", ["verbose", "help", "uid=", "process_id=", "upload=", "metadata="] ) for o, a in opts: if o in ("-h", "--help"): usage() elif o in ("-v", "--verbose"): verbose = True elif o in ("-i", "--uid"): uid = str(a) elif o in ("-p", "--process_id"): process_id = str(a) elif o in ("-u", "--upload"): audioFilename = str(a) elif o in ("-m", "--metadeta"): metadataFilename = str(a) else: print "Wrong option " + o + "\n" usage() if len(args) < 2: print "You need to provide an object type and action!" usage() object_type = args[0] action = args[1] if len(args) == 3: accept = args[2] register_openers() # Create an instance of the <object_type> given as input argument with the provided arguments inst = globals()[object_type](accept, username, password, uid, process_id, audioFilename, metadataFilename) # Call the <action> indicated in the input arguments func = getattr(inst, action) func() print >> sys.stderr, "----------- response ----------" print >> sys.stderr, inst.response.code, inst.response.msg print >> sys.stderr, "----------- headers ----------" print >> sys.stderr, inst.response.headers print >> sys.stderr, "-------- body --------" print inst.response.read()
def main(argv=None): if argv is None: argv = sys.argv verbose = False uid = None process_id = None audioFilename = None metadataFilename = None transcriptFilename = None accept = 'text/xml' service = None item_id = None count = None status = None # NEED TO SPECIFIY USERNAME AND PASSWORD HERE username = '******' password = '******' opts, args = getopt.getopt(argv[1:], "vhi:p:u:m:s:e:t:f:", ["verbose" ,"help", "uid=", "process_id=", "upload=", "metadata=","service=","item_id=","transcript=","filter="]) for o, a in opts: if o in ("-h","--help"): usage() elif o in ("-v","--verbose"): # TODO: the goggles do nothing! verbose = True elif o in ("-i", "--uid"): uid = str(a) elif o in ("-p", "--process_id"): process_id = str(a) elif o in ("-u", "--upload"): audioFilename = str(a) elif o in ("-m", "--metadeta"): metadataFilename = str(a) elif o in ("-t","--transcript"): transcriptFilename = str(a) elif o in ("-s", "--service"): service = str(a) elif o in ("-e","--item_id"): item_id = str(a) elif o in ("-c","--count"): count = int(a) elif o in ("-f","--filter"): a = a.upper() if a in STATUS_CODE: status = STATUS_CODE[a] else: raise Exception("Unrecognised STATUS from %s" % STATUS_LIST) else: print 'Wrong option '+o+'\n' usage() if len(args) < 2: print 'You need to provide an object type and action!' usage() object_type = args[0] action = args[1] if len(args) == 3: accept = args[2] register_openers() # Create an instance of the <object_type> given as input argument with the provided arguments inst = globals()[object_type](accept, username, password, uid, process_id, audioFilename, metadataFilename, transcriptFilename, service, item_id, count, status) # Call the <action> indicated in the input arguments func = getattr(inst, action) func() print >> sys.stderr,"----------- response ----------" print >> sys.stderr,inst.response.code, inst.response.msg print >> sys.stderr,"----------- headers ----------" print >> sys.stderr,inst.response.headers print >> sys.stderr,"-------- body --------" print inst.response.read()
def main(): username = "" password = "" from Media import Media register_openers() # 1) Upload a media file inst = Media(accept="text/xml", username=username, password=password, audioFilename="test.mp3") inst.create() # extract the uid given to this media item if inst.response.code == 200: print "Media item has been created successfully" search = None search = re.search("<id>(.*)</id>", inst.response.read()) if search != None: uid = search.group(1) uid = str(uid) print "The following uid has been extracted: %s" % uid else: print >> sys.stderr, "An error occured trying to extract the uid" else: print >> sys.stderr, "-------- An error occurred, response: --------" print >> sys.stderr, inst.response.code, inst.response.msg print >> sys.stderr, "-------- Headers --------" print >> sys.stderr, inst.response.headers inst.uid = uid # 2) Request a transcript of the just uploaded media item inst.transcribe() # extract the process id given to this process: if inst.response.code == 202: print "Transcription has been accepted" search = None search = re.search('<atom:link href="https://www.koemei.com/REST/media/.*/transcribe/(.*)" rel="self"></atom:link>', inst.response.read()) if search != None: process_id = search.group(1) print "The following process id has been extracted: %s" % process_id else: print >> sys.stderr, "An error occured trying to extract the process id" else: print >> sys.stderr, "-------- An error occurred, response: --------" print >> sys.stderr, inst.response.code, inst.response.msg print >> sys.stderr, "-------- Headers --------" print >> sys.stderr, inst.response.headers time.sleep(5) from Process import Process inst = Process(accept="text/xml", username=username, password=password, uid=uid, process_id=process_id) transcript_ready = False while (transcript_ready == False): inst.get() # extract the progress of this process and if completed the transcript id: if inst.response.code == 200: transcript = open('transcript.xml', 'w+') transcript.write(inst.response.read()) transcript.close() transcript = open('transcript.xml', 'r') for e in transcript.readlines(): e = e.strip() search = None search = re.search('<progress>(.*)</progress>', e) if search != None: print "Transcription still in progress" progress = search.group(1) print "The process is at %s %%" % progress break transcript.close() # if no progress info has been found, check if the transcript is ready: if search == None: transcript = open('transcript.xml', 'r') for e in transcript.readlines(): e = e.strip() search = None search = re.search('<segmentation>', e) if search != None: transcript_ready = True print "Transcription has finished, the transcript has been saved to transcript.xml" break transcript.close() if search == None: print >> sys.stderr, "An error occured trying to extract the progress" else: print >> sys.stderr, "-------- An error occurred, response: --------" print >> sys.stderr, inst.response.code, inst.response.msg print >> sys.stderr, "-------- Headers --------" print >> sys.stderr, inst.response.headers if transcript_ready == False: time.sleep(600)
def API_LoadLoggerData(LoggerFile, site_id, api, project, template_id): from encode import multipart_encode from streaminghttp import register_openers import urllib2, os, csv class Test: def __init__(self): self.contents = '' def body_callback(self, buf): self.contents = self.contents + buf print LoggerFile a = open(LoggerFile, 'rb') logger = csv.reader(a) UpdateFile = 'Updater.csv' b = open(UpdateFile, 'rb') update = csv.reader(b) c = open('Upload_File.csv','wb') upload_data = csv.writer(c, delimiter=',',quoting=csv.QUOTE_NONE) data = list(logger) up = list(update) diff = len(data)-len(up) print "# New Data Rows=", diff if diff > 0: print "Creating Upload File" startline = 1 for row in data[-(diff):]: upload_data.writerow(row) a.close() b.close() c.close() upload_file = os.getcwd()+'\Upload_File.csv' print "File to Upload to VOEIS="+upload_file url='https://voeis.msu.montana.edu/projects/'+str(project)+'/apivs/upload_data.json?' print 'URL='+url params = ({'datafile': open('Upload_File.csv', 'rb'), 'data_template_id': template_id, 'site_id': site_id, 'start_line': startline, 'api_key': api }) # Register the streaming http handlers with urllib2 register_openers() # headers contains the necessary Content-Type and Content-Length # datagen is a generator object that yields the encoded parameters datagen, headers = multipart_encode(params) print headers # Create the Request object request = urllib2.Request(url, datagen, headers) # Actually do the request, and get the response result = urllib2.urlopen(request).read() print result return result
def main(): register_openers() upload_transcribe(audioFilename="test.mp3")