Example #1
0
    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(**kwargs)
        # Youtubeインスタンス作成

        credentials_path = "credentials.json"
        if os.path.exists(credentials_path):
            # 認証済み
            store = Storage(credentials_path)
            credentials = store.get()
        else:
            # 認証処理
            f = "/code/VisualizingYoutube/client.json"
            scope = "https://www.googleapis.com/auth/youtube.readonly"
            flow = client.flow_from_clientsecrets(f, scope)
            flow.user_agent = "YouTubeLiveStreaming"
            credentials = tools.run_flow(flow, Storage(credentials_path))

        # search_response = youtube.search().list(
        #     # q='ThtcrBgB4KY',
        #     regionCode='JP',
        #     eventType='live',
        #     type='video',
        #     part='id,snippet',
        #     order='viewCount',
        #     maxResults=10,
        # ).execute()

        # print(search_response)

        # context = {
        #     'sorted_df': sorted_df
        # }

        return context
Example #2
0
def get_credentials():
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'sheets.googleapis.com-singer-target.json')

    store = Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else:  # Needed only for compatibility with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials
def get_authenticated_service():  # Modified
    credential_path = os.path.join('./', 'credentials.json')
    store = Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRETS_FILE, SCOPES)
        credentials = tools.run_flow(flow, store)
    return build(API_SERVICE_NAME, API_VERSION, credentials=credentials)
Example #4
0
def create_post(request, id):
    global GMAIL
    try:
        store = file.Storage('WikiBreach/gmail.json')
        creds = store.get()
        if not creds or creds.invalid:
            flow = client.flow_from_clientsecrets(
                'WikiBreach/client_secret.json', 'https://mail.google.com/')
            creds = tools.run_flow(flow, store)
        GMAIL = discovery.build('gmail', 'v1', http=creds.authorize(Http()))
    except errors.HttpError as error:
        print('An error occurred: %s' % error)

    try:
        message_body = GMAIL.users().messages().get(
            userId='*****@*****.**', id=id).execute()
        data = message_body['payload']
        msg = data['parts']
        part = msg[1]
        body = part['body']
        dat = body['data']
        msg_str = base64.urlsafe_b64decode(dat.encode('UTF-8'))
        soup = BeautifulSoup(msg_str, "lxml")
        d = str(soup.find_all("script"))
        m = []
        m = d.split("type=\"application/json\">", 1)
        p = m[1].split("</script>]", 1)
        msg_json = str(p[0])
        data2 = json.loads(msg_json)
        date = data2['entity']['subtitle']
        dateArray = date.split("Latest: ")
        date = dateArray[1]
        date = parser.parse(date).strftime('%Y-%m-%d')
        keyword = str(data2['entity']['title']).split("Google Alert - ")[1]
        widgets = data2["cards"][0]
        widgets = widgets['widgets'][0]
        title = widgets['title']
        description = widgets['description']
        source_url = widgets['url']
        return render(
            request, 'createPost.html', {
                'title': title,
                'date': date,
                'keyword': keyword,
                'description': description,
                'sourcelink': source_url,
                'message_id': id
            })
    except errors.HttpError as error:
        print('An error occurred: %s' % error)
Example #5
0
def get_credentials():
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'drive-python-quickstart.json')
    store = Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else:
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials
Example #6
0
    def get_authenticated_service(self):
        """ 
            Gets an authenticated service object for requests to the 
            YouTube Data API
        """

        store = file.Storage("youtube_video_manager-oauth2.json")

        creds = store.get()

        if creds is None or creds.invalid:
            flow = client.flow_from_clientsecrets(
                self.CLIENT_SECRETS_FILE,
                scope=self.YOUTUBE_UPLOAD_SCOPE,
                message=self.MISSING_CLIENT_SECRETS_MESSAGE)
            creds = tools.run_flow(flow, store, cmd_flags())

        return build(self.YOUTUBE_API_SERVICE_NAME,
                     self.YOUTUBE_API_VERSION,
                     http=creds.authorize(httplib2.Http()))
Example #7
0
    _create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
    # Legacy Python that doesn't verify HTTPS certificates by default
    pass
else:
    # Handle target environment that doesn't support HTTPS verification
    ssl._create_default_https_context = _create_unverified_https_context

conn = sqlite3.connect("vouchers.db")

# Setup the Gmail API
SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store)
service = build('gmail', 'v1', http=creds.authorize(Http()))

# Call the Gmail API
category = service.users().labels().list(userId='me').execute()

# Call the Gmail API, only get 1 of the the recent message ids
# First get the message id for the message
results = service.users().messages().list(userId='me',
                                          q='in:SZMPESA',
                                          maxResults=1).execute()

# get the message id from the results object
message_id = results['messages'][0]['id']