def search(request): if request.method == 'GET': q = "" try: q = request.GET["q"] except: return HttpResponseRedirect('/search') results = Entry.objects.filter( Q(title__icontains=q) | Q(narrative__icontains=q)).distinct() results = results.filter(show=True) scribd.config(settings.SCRIBD_KEY, settings.SCRIBD_SEC) scribd_user = scribd.login(settings.SCRIBD_USER, settings.SCRIBD_PASS) scribd_docs = scribd.find(query=q, scope="user") if scribd_docs: for doc in scribd_docs: scribd_id = str(doc._get_id()) try: f = File.objects.get(scribd_id = scribd_id) more_results = Entry.objects.filter(id=f.entry.id, show=True) results = list(results) + list(more_results) except: pass # la-ame! results = list(set(results)) return render_to_response('results.html', { 'results': results, 'query': q}) else: #no query. return render_to_response('search.html', { })
def main(): # Configure the Scribd API. scribd.config(API_KEY, API_SECRET) try: # Log the user in. user = scribd.login(USERNAME, PASSWORD) # Get all documents uploaded by the user. docs = user.all() print('User %s has %d documents.' % (user.username, len(docs))) if docs: print("User's documents:") for doc in docs: print('*', doc.title) # Search the user documents for the phrase "checklist". results = user.find('checklist') print('Search for "checklist" turned up %d results:' % len(results)) for doc in results: print('*', doc.title) except scribd.ResponseError as err: print('Scribd failed: code=%d, error=%s' % (err.errno, err.strerror))
def config(self, key, secret): # Connection information scribd.config(key, secret) # Width and height per target platform = Commander().opts.platform self.desired_size = self.sizes[platform]
def upload_from_url(request,url): scribd.config(API_KEY, API_SECRET) try: # Upload the file to Scribd. document = scribd.api_user.upload_from_url(url, access='private') ret=document.get_scribd_url() document.save() except scribd.ResponseError, err: pass
def update_descriptions(API_KEY, API_SECRET): scribd.config(API_KEY, API_SECRET) for doc in scribd.api_user.all(): record = models.Record.query.filter_by(doc_id = doc.id).first() if record: link_back = app.config['APPLICATION_URL'] + 'request/' + str(record.request_id) description = "This document was uploaded via RecordTrac in response to a public records request for the %s. You can view the original request here: %s" % ( app.config['AGENCY_NAME'], link_back) doc.description = description doc.save() app.logger.info("\n\nUpdated Scribd document %s's description to %s" %(doc.id, description))
def get_scribd_download_url(doc_id, record_id = None): if not should_upload(): return None API_KEY = app.config['SCRIBD_API_KEY'] API_SECRET = app.config['SCRIBD_API_SECRET'] try: scribd.config(API_KEY, API_SECRET) doc = scribd.api_user.get(doc_id) doc_url = doc.get_download_url() if record_id: set_scribd_download_url(download_url = doc_url, record_id = record_id) return doc_url except: return None
def Upload_from_file(request,file): """Passes the uploaded file to Scribd and returns scribd link.""" scribd.config(API_KEY, API_SECRET) try: # Upload the file to Scribd. document = scribd.api_user.upload(file, access='private') ret=document.get_scribd_url() document.save() # Redirect back to the main page. except scribd.ResponseError, err: pass
def get_docs(request,query): # Configure the Scribd API. scribd.config(API_KEY, API_SECRET) try: # Log the user in. user = scribd.login(USERNAME, PASSWORD) # Get all documents uploaded by the user. docs = user.all() # Search the user documents for the phrase query results = user.find('checklist') except scribd.ResponseError, err: print 'Scribd failed: code=%d, error=%s' % (err.errno, err.strerror)
def upload(file, filename, API_KEY, API_SECRET): # Configure the Scribd API. scribd.config(API_KEY, API_SECRET) doc_id = None try: # Upload the document from a file. doc = scribd.api_user.upload(targetfile=file, name=filename, progress_callback=progress, req_buffer=tempfile.TemporaryFile()) doc_id = doc.id return doc_id except scribd.ResponseError, err: print 'Scribd failed: code=%d, error=%s' % (err.errno, err.strerror) return err.strerror
def upload(file, filename, API_KEY, API_SECRET, description): # Configure the Scribd API. scribd.config(API_KEY, API_SECRET) doc_id = None try: # Upload the document from a file. doc = scribd.api_user.upload( targetfile=file, name=filename, progress_callback=progress, req_buffer=tempfile.TemporaryFile() ) doc.description = description doc.save() doc_id = doc.id return doc_id except scribd.ResponseError, err: app.logger.info("Scribd failed: code=%d, error=%s" % (err.errno, err.strerror)) return err.strerror
def scribd_embed(file): file_contents = file.read() key = hashlib.sha512(file_contents).hexdigest() try: document = ScribdDocument.objects.get(key=key) except: scribd.config(SCRIBD_API_KEY, SCRIBD_API_SECRET) uploaded = scribd.api_user.upload(open(file.path, 'rb'), access='private') attributes = uploaded.get_attributes() document = ScribdDocument( key = key, document_id = attributes['doc_id'], access_key = attributes['access_key'], secret_password = attributes['secret_password'] ) document.full_clean() document.save() return render_to_string('review/assets/scribd_embed.html', {'document': document})
def save(self): scribd.config(API_KEY, API_SECRET) ###upload doc to scribd for conversion### try: basename,name=self.document.path.rsplit('/',1) doc = scribd.api_user.upload(open(MEDIA_ROOT+'/Docs/'+name)) self.scribd_id=doc.id self.scribd_secret=doc.access_key doc.title = self.title doc.description = self.description doc.access = self.access doc.language = self.language doc.tags = self.tag_list doc.show_ads = self.show_ads # Commit all above changes. doc.save() except scribd.ResponseError, err: print 'Scribd failed: code=%d, error=%s' % (err.errno, err.strerror)
def main(): # Configure the Scribd API. scribd.config(API_KEY, API_SECRET) try: # Upload the document from a file. print 'Uploading a document...' # Note that the default API user object is used. doc = scribd.api_user.upload( open('test.txt','rb'), progress_callback=progress, req_buffer = tempfile.TemporaryFile() ) print 'Done (doc_id=%s, access_key=%s).' % (doc.id, doc.access_key) # Poll API until conversion is complete. while doc.get_conversion_status() != 'DONE': print 'Document conversion is processing...' # Sleep to prevent a runaway loop that will block the script. time.sleep(2) print 'Document conversion is complete.' # Edit various document options. # (Note that the options may also be changed during the conversion) doc.title = 'This is a test document!' doc.description = "I'm testing out the Scribd API!" doc.access = 'private' doc.language = 'en' doc.license = 'c' doc.tags = 'test,api' doc.show_ads = 'true' # Commit all above changes. doc.save() # Delete the uploaded document. print 'Deleting the document...' doc.delete() print 'Done (doc_id=%s).' % doc.id except scribd.ResponseError, err: print 'Scribd failed: code=%d, error=%s' % (err.errno, err.strerror)
def scribd_embed(file): file_contents = file.read() key = hashlib.sha512(file_contents).hexdigest() try: document = ScribdDocument.objects.get(key=key) except: scribd.config(SCRIBD_API_KEY, SCRIBD_API_SECRET) uploaded = scribd.api_user.upload(open(file.path, 'rb'), access='private') attributes = uploaded.get_attributes() document = ScribdDocument( key=key, document_id=attributes['doc_id'], access_key=attributes['access_key'], secret_password=attributes['secret_password']) document.full_clean() document.save() return render_to_string('review/assets/scribd_embed.html', {'document': document})
def save(self): super(Tutorial, self).save() scribd.config(settings.SCRIBD_API_KEY, settings.SCRIBD_API_SECRET) if self.slides and self.upload_to_scribd: self.upload_to_scribd = False try: print "Uploading %s to Scribd" % self.slides doc = scribd.api_user.upload(open(self.slides.path)) self.scribd_doc_id = doc.id self.scribd_doc_key = doc.access_key doc.title = self.title doc.description = self.title doc.access = 'public' doc.language = 'en' doc.tags = 'netsoc' doc.show_ads = 'false' doc.save() super(Tutorial, self).save() except scribd.ResponseError, err: print 'Scribd failed: code=%d, error=%s' % (err.errno, err.strerror)
def upload(filepath, API_KEY, API_SECRET): # Configure the Scribd API. scribd.config(API_KEY, API_SECRET) doc_id = None try: # Upload the document from a file. doc = scribd.api_user.upload( open(filepath,'rb'), progress_callback=progress, req_buffer = tempfile.TemporaryFile() ) # Poll API until conversion is complete. while doc.get_conversion_status() != 'DONE': # Sleep to prevent a runaway loop that will block the script. time.sleep(2) doc_id = doc.id return doc_id except scribd.ResponseError, err: print 'Scribd failed: code=%d, error=%s' % (err.errno, err.strerror) return err.strerror
def make_private(doc_id, API_KEY, API_SECRET): scribd.config(API_KEY, API_SECRET) doc = scribd.api_user.get(doc_id) doc.access = 'private' doc.save()
import scribd scribd.config('your_api_key', 'your_api_secret')
def make_public(doc_id, API_KEY, API_SECRET): scribd.config(API_KEY, API_SECRET) doc = scribd.api_user.get(doc_id) doc.access = "public" doc.save()
def add(request): context = {} entry_form = EntryForm(prefix='entries') file_forms_excludes = ('entry', 'scribd_link', 'scribd_ak', 'size', 'scribd_id', 'name') FileFormSetFactory = modelformset_factory(File, form=FileForm, extra=8, exclude=file_forms_excludes,) file_formset = FileFormSetFactory(prefix='files', queryset=File.objects.none()) if request.method == 'POST': entry_form = EntryForm(request.POST, request.FILES, prefix='entries') file_formset = FileFormSetFactory(request.POST, request.FILES, prefix='files', queryset=File.objects.none()) if entry_form.is_valid() and file_formset.is_valid(): entry = entry_form.save(commit=False) entry.poster_slug = slugify(entry.poster) entry.slug = slugify(entry.title) ''' The name of the entity has has been sent as a string. We need to use an actual entity object. ''' entity_name = request.POST['entries-govt_entity'] try: entity = Entity.objects.get(name=entity_name) entry.entity = entity except Entity.DoesNotExist: entity = Entity(name=entity_name) entity.slug = slugify(entity.name) entity.save() entry.entity = entity # Now we can actually save. entry.save() # saving the files - more involved scribd.config(settings.SCRIBD_KEY, settings.SCRIBD_SEC) scribd_user = scribd.login(settings.SCRIBD_USER, settings.SCRIBD_PASS) for f in file_formset.save(commit=False): f.name = f.theFile.name.split("/")[-1] f.size = convert_bytes(f.theFile.size) f.entry = entry f.scribd_link = "" f.scribd_id = "" f.scribd_ak = "" # attempt to upload it to scribd try: scribd_doc = scribd_user.upload(f.theFile) f.scribd_id = str(scribd_doc._get_id()) f.scribd_link = scribd_doc.get_scribd_url() f.scribd_ak = scribd_doc.access_key except scribd.ResponseError: pass # TODO handle this in a more reasonable way f.save() return HttpResponseRedirect('/doc/' + str(entry.id)) context['fileform'] = file_formset context['entryform'] = entry_form context['entities'] = entities() return render_to_response('add.html', context)
def post(self): # Get the cStringIO object containing the file data. file = self.request.POST.get('file').file # Get the name of the uploaded file. name = self.request.POST.get('file').filename # Upload the file to Scribd. document = scribd.api_user.upload(file, name, access='private') # Set the description. document.description = self.request.get('description') document.save() # Redirect back to the main page. self.redirect('/') application = webapp.WSGIApplication([ ('/', Form), ('/upload', Upload), ], debug=True) if __name__ == '__main__': # Set the Scribd API key and API secret. scribd.config(API_KEY, API_SECRET) # Start the application. run_wsgi_app(application)
def upload_to_scribd(self): scribd.config(settings.SCRIBD_KEY, settings.SCRIBD_SECRET) filename = '/tmp/%s.pdf' % self.form_id pdf = pyPdf.PdfFileReader( StringIO(urllib2.urlopen(self.house_pdf_url()).read())) try: pdf.decrypt(r'') # Encrypted with a blank password except KeyError: pass output = pyPdf.PdfFileWriter() for pagenum in range(pdf.getNumPages()): output.addPage(pdf.getPage(pagenum)) outputStream = open(filename, 'wb') output.write(outputStream) outputStream.close() fh = open(filename, 'rb') doc = scribd.api_user.upload(fh, access='private') fh.close() os.remove(filename) site = Site.objects.get_current() description = 'This registration was filed with the House' if self.signed_date: description += ' on %s' % self.signed_date.strftime('%B %d, %Y') else: description += ' in %s' % self.report_year params = { 'title': 'Registration by %s to lobby for %s (%s)' % (self.organization.name, self.client.name, self.form_id), 'description': description, 'link_back_url': 'http://%s%s' % (site.domain, self.get_absolute_url()), 'category': 'Government Docs', 'access': 'public', } scribd.update([ doc, ], **params) collections = scribd.api_user.get_collections() collection = [ x for x in collections if x.collection_name == 'Lobbyist Registrations' ] if collection: collection = collection[0] try: doc.add_to_collection(collection) except: pass self.scribd_id = doc.id self.scribd_url = doc.get_scribd_url() self.scribd_access_key = doc.get_attributes()['access_key'] self.save() return doc
def main(): usage = """Usage: - to upload a file in scribd python scridbc.py -K <api key> -S <api secret> -i <input filename> -t <title> - to delete an existing file python scridbc.py -K <api key> -S <api secret> -c <doc_id> -d - to convert any file (doc,xls,ppt,etc.) to pdf python scridbc.py -K <api key> -S <api secret> -i <input filename> -p <output> -d - to convert any file (doc,xls,ppt,etc.) to txt python scridbc.py -K <api key> -S <api secret> -i <input filename> -x <output> -d """ parser = optparse.OptionParser(usage=usage) parser.add_option("-K", "--key", dest='key',default='', help="the API KEY (required)") parser.add_option("-S", "--secret", dest='secret',default='', help="the API SECRET (required)") parser.add_option("-c", "--code", dest="code", help="document code") parser.add_option("-i", "--input", dest="input",default=None, help="input filename to upload") parser.add_option("-t", "--title", dest="title",default='', help="document title") parser.add_option("-l", "--license", dest="license",default='c', help="document license") parser.add_option("-L", "--language", dest="language",default='en', help="document language") parser.add_option("-D", "--description", dest="description",default='', help="file with document description") parser.add_option("-m", "--mode", dest="mode",default='public', help="public or private") parser.add_option("-g", "--tags", dest="tags",default='', help="comma separated tags") parser.add_option("-p", "--pdf", dest="pdf",default=None, help="output pdf filename") parser.add_option("-x", "--txt", dest="txt",default=None, help="output txt filename") parser.add_option("-d", "--delete", action="store_true", dest="delete", default=False, help="delete the document") parser.add_option("-a", "--ads", dest='ads',default='on', help="ads on or off") (options, args) = parser.parse_args() # Configure the Scribd API. scribd.config(scribd_settings.API_KEY, scribd_settings.API_SECRET) try: if options.input: # Upload the document from a file. print 'Uploading a document...' # Note that the default API user object is used. doc = scribd.api_user.upload(open(options.input,'rb')) print 'Done (doc_id=%s, access_key=%s).' % (doc.id, doc.access_key) # Poll API until conversion is complete. n=2 while doc.get_conversion_status() != 'DONE': print 'Document conversion is processing... (retrying in %ssecs)' % n # Sleep to prevent a runaway loop that will block the script. time.sleep(n) n *= 2 print 'Document conversion is complete.' # Edit various document options. # (Note that the options may also be changed during the conversion) doc.title = options.title or options.input if options.description and os.path.exists(options.description): doc.description = open(options.description,'r').read() else: doc.description = '' doc.access = options.mode doc.language = options.language doc.license = options.license doc.tags = options.tags doc.show_ads = 'true' if options.ads=='on' else 'false' # Commit all above changes. doc.save() doc.get_download_url(doc_type='pdf') print 'Document uploaded (doc_id=%s).' % doc.id elif options.code: doc = scribd.api_user.get(options.code) print 'Document found (doc_id=%s).' % doc.id else: print 'Invalid arguments' return pdf = doc.get_download_url(doc_type='pdf') print 'PDF URL: '+pdf if options.pdf: open(options.pdf,'wb').write(urllib.urlopen(pdf).read()) print 'PDF file retrieved' txt = doc.get_download_url(doc_type='txt') print 'TXT URL: '+txt if options.txt: open(options.txt,'wb').write(urllib.urlopen(txt).read()) print 'TXT file retrieved' if options.delete: doc.delete() print 'Document deleted (doc_id=%s).' % doc.id except scribd.ResponseError, err: print 'Scribd failed: code=%d, error=%s' % (err.errno, err.strerror)