Exemplo n.º 1
0
 def post(self):
     client = gdata.sites.client.SitesClient(source='rosefiji-fijifiles-v1',
                                             site='fiji-files',
                                             domain='rosefiji.com')
     import os.path
     folder = os.path.dirname(os.path.realpath(__file__))
     file_path = os.path.join(folder, 'credentials.json')
     credentials = ServiceAccountCredentials.from_json_keyfile_name(
         file_path, scopes)
     credentials._kwargs['sub'] = '*****@*****.**'
     auth2token = gdata.gauth.OAuth2TokenFromCredentials(credentials)
     auth2token.authorize(client)
     dept = self.request.params.get('department')
     file_id = models.get_upload_number()
     uri = '%s?kind=%s' % (client.MakeContentFeedUri(),
                           'filecabinet&path=/' + dept)
     feed = client.GetContentFeed(uri=uri)
     f = self.request.params.get('file')
     ext = f.filename.split(".")[-1].upper()
     media = gdata.data.MediaSource(
         file_handle=f.file.read(),
         content_type=gdata.docs.service.SUPPORTED_FILETYPES.get(ext)
         or 'application/octet-stream',
         content_length=self.getSize(self.request.params.get('file').file))
     try:
         urlfetch.set_default_fetch_deadline(30)
         attachment = client.UploadAttachment(
             media,
             feed.entry[0],
             title=file_id,
             description='UPLOADED TO NEW FIJI FILES SITE')
     except Exception, e:
         logging.exception(e)
         self.redirect('/upload?msg=retry')
         return
Exemplo n.º 2
0
def upload(url, title, price, body, imgList):
    try:
        # Call an API e.g. to get the site content feed
        uri = '%s?path=%s' % (client.MakeContentFeedUri(), '/today-s-listings')

        feed = client.GetContentFeed(uri=uri)

        full_body = "<p>" + body + "</p><br>"

        for img in imgList:
            full_body += '<img style="display:block;margin-right:auto;margin-left:auto;text-align:center" src="' + img + '"><br>'

        full_title = title + " - " + price

        entry = client.CreatePage('webpage',
                                  full_title,
                                  html=full_body,
                                  parent=feed.entry[0])

        google_sheet.update_post(url, entry.GetAlternateLink().href)

        # sys.stdout.write('Created. View it at: %s' % entry.GetAlternateLink().href)
        print('Created. View it at: %s' % entry.GetAlternateLink().href)

    except Exception:
        pass
        # sys.stdout.write("Error posting, please check google sheet for duplicates: " + url)
        print("Error posting, please check google sheet for duplicates: " +
              url)
Exemplo n.º 3
0
        seq1.extend([x for x in seq2 if x not in seen])
    return seq1


if __name__ == "__main__":
    # Connect to google site
    SOURCE_APP_NAME = 'dongningInvest-invelon-v1'

    client = gdata.sites.client.SitesClient(source=SOURCE_APP_NAME,
                                            site='investelon',
                                            domain=None)
    client.ClientLogin('*****@*****.**', 'Ljgoogle2254',
                       client.source)

    uri = '%s?kind=%s' % (client.make_content_feed_uri(), 'webpage')
    feed = client.GetContentFeed(uri=uri)
    old_entry = feed.entry[0]

    contractDict = {}
    # Note: Option quotes will give an error if they aren't shown in TWS
    contractDict[0] = ('^GSPC', 'IND', 'CBOE', 'USD', '', 0.0, '')
    contractDict[1] = ('^IXIC', 'IND', 'NASDAQ', 'USD', '', 0.0, '')
    contractDict[2] = ('^DJI', 'IND', 'NYSE', 'USD', '', 0.0, '')
    contractDict[3] = ('^RUT', 'IND', 'NYSE', 'USD', '', 0.0, '')

    contractDict[4] = ('TSLA', 'STK', 'SMART', 'USD', '', 0.0, '')
    contractDict[5] = ('SCTY', 'STK', 'SMART', 'USD', '', 0.0, '')
    contractDict[6] = ('FEYE', 'STK', 'SMART', 'USD', '', 0.0, '')
    contractDict[7] = ('DXCM', 'STK', 'SMART', 'USD', '', 0.0, '')
    contractDict[8] = ('AAPL', 'STK', 'SMART', 'USD', '', 0.0, '')
    contractDict[9] = ('MDSO', 'STK', 'SMART', 'USD', '', 0.0, '')
Exemplo n.º 4
0
# This is based on information in this blog post:
# https://groups.google.com/forum/m/#!msg/google-apps-developer-blog/1pGRCivuSUI/3EAIioKp0-wJ

auth2token = gdata.gauth.OAuth2Token(client_id=credentials.client_id,
                                     client_secret=credentials.client_secret,
                                     scope=SCOPE,
                                     access_token=credentials.access_token,
                                     refresh_token=credentials.refresh_token,
                                     user_agent='sites-test/1.0')

# Create a gdata client

client = gdata.sites.client.SitesClient(source='sites-test',
                                        site='YOUR_SITE',
                                        domain='YOUR_DOMAIN',
                                        auth_token=auth2token)

# Authorize it

auth2token.authorize(client)

# Call an API e.g. to get the site content feed

feed = client.GetContentFeed()

for entry in feed.entry:
    print '%s [%s]' % (entry.title.text, entry.Kind())

# See:
# https://developers.google.com/google-apps/sites/docs/1.0/developers_guide_python
# for more details of the Sites API
def main():
    site_domain = 'votca.org'

    from optparse import OptionParser
    usage = "usage: %prog [options] arg"
    parser = OptionParser(usage)
    parser.add_option("-u", "--user", dest="user", help="username for login")
    parser.add_option("-p",
                      "--passwd",
                      dest="password",
                      help="password for login")
    parser.add_option("-s",
                      "--site",
                      dest="site",
                      help="site to upload [default='main']")
    parser.add_option("-d", "--dir", dest="directory", help="path to site")
    parser.add_option("-f",
                      "--file",
                      dest="filename",
                      help="local file to upload to the site")
    parser.add_option("-a",
                      "--attach",
                      dest="attachment",
                      help="add a file as attachment")
    #parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=True)
    #parser.add_option("-q", "--quiet", action="store_false", dest="verbose")

    (options, args) = parser.parse_args()

    # parse options
    if (options.user == None):
        login_name = raw_input("username: "******"password: "******"Error: Path to site is missing")
    else:
        page_path = options.directory

    if (options.filename != None):
        filename = options.filename

    # proceed to upload
    debug = False
    ssl = True

    # create Sitesclient
    print "Logging in as " + login_name

    client = gdata.sites.client.SitesClient(source=SOURCE_APP_NAME,
                                            site=site_name,
                                            domain=site_domain)
    client.http_client.debug = debug
    client.ssl = ssl

    # login to google sites
    try:
        client.ClientLogin(login_name, login_password, client.source)
    except gdata.client.BadAuthentication:
        exit('Error: Invalid user credentials given.')
    except gdata.client.Error:
        exit('Error: Login Error')

    # try to update page
    try:
        print "Updating content on site: " + page_path
        # try to fetch site
        uri = '%s?path=%s' % (client.MakeContentFeedUri(), page_path)
        feed = client.GetContentFeed(uri=uri)
        entry = feed.entry[0]
        entry_copy = entry

        # update site content
        if (options.filename != None):
            f = open(filename, 'r')
            entry.content = gdata.atom.data.Content(f.read())
            f.close()
            client.Update(entry)
            print "Website updated from file: " + options.filename
        else:
            print "Note: No file given. Website content remains unchanged."

        # update attachment
        if (options.attachment != None):
            ms = gdata.data.MediaSource(file_path=options.attachment,
                                        content_type="text/ascii")

            # find attachment
            uri = "%s?kind=%s" % (client.MakeContentFeedUri(), "attachment")
            feed = client.GetContentFeed(uri=uri)
            existing_attachment = None
            for entry in feed.entry:
                if entry.title.text == options.attachment:
                    existing_attachment = entry

            if existing_attachment is not None:
                existing_attachment.summary.text = options.attachment
                updated = client.Update(existing_attachment, media_source=ms)
                print "New attachment uploaded: " + updated.GetAlternateLink(
                ).href
            else:
                # find cabinet
                #uri = "%s?kind=%s" % (client.MakeContentFeedUri(), "filecabinet")
                #feed = client.GetContentFeed(uri=uri)
                #cabinet = None
                #for entry in feed.entry:
                #  if entry.title.text == options.attachment:
                #    cabinet = entry
                cabinet = entry_copy
                if cabinet is None:
                    exit("Error: Cabinet does not exist.")
                attachment = client.UploadAttachment(ms,
                                                     cabinet,
                                                     title=options.attachment)
                print "Existing attachment has been updated: ", attachment.GetAlternateLink(
                ).href
        #print "pushed update to  " + entry.GetAlternateLink().href
        print "Done."
    except gdata.client.RequestError, error:
        print error