예제 #1
0
def callback():
    if session.get('error', None):
        return redirect('/')

    if session['state'] != request.args.get('state', None):
        return redirect('/')

    tokens = google_api.get_oauth_tokens(
        url_for('server.routes.auth.callback', _external=True),
        request.args['code'])
    email = google_api.get_user_email(tokens['access_token'])
    user = User.get(email)

    if not user:
        user = User(_id=email,
                    subscriptions=[],
                    collections={},
                    credentials=google_api.create_credentials(tokens),
                    job_id=None,
                    last_updated=datetime.utcnow())
        job = queue.enqueue('server.utils.google_api.build_collections',
                            access_token=tokens['access_token'],
                            username=email)
        user.job_id = job.id
        user.insert()
    else:
        user.update({'credentials': google_api.create_credentials(tokens)})

    login_user(user, remember=True, duration=timedelta(30))

    return redirect('/')
예제 #2
0
def load_user(email):
    return User.get(email)
예제 #3
0
def build_collections(access_token: str, username: str):
    subs, info = [], {'nextPageToken': ''}
    q = Queue()
    threads = []

    params = {
        'part': 'snippet',
        'mine': True,
        'maxResults': 50,
        'pageToken': info['nextPageToken']
    }

    while 'nextPageToken' in info:
        info = get(url='https://www.googleapis.com/youtube/v3/subscriptions',
                   headers={
                       'Authorization': f'Bearer {access_token}'
                   },
                   params={
                       **params, 'pageToken': info['nextPageToken']
                   }).json()
        subs.append({
            item['snippet']['resourceId']['channelId']: None
            for item in info['items']
        })
        threads.append(Thread(target=getTopics, args=(subs[-1].keys(), q)))
        threads[-1].start()

    topic_map = {
        '/m/04rlf': 'Music',
        '/m/0bzvm2': 'Gaming',
        '/m/06ntj': 'Sports',
        '/m/02jjt': 'Entertainment',
        '/m/019_rr': 'Lifestyle',
        '/m/01k8wb': 'Knowledge',
        '/m/098wr': 'Society'
    }

    collections = {
        'Music': [{}],
        'Gaming': [{}],
        'Sports': [{}],
        'Entertainment': [{}],
        'Lifestyle': [{}],
        'Knowledge': [{}],
        'Society': [{}]
    }

    pages = {
        'Music': 0,
        'Gaming': 0,
        'Sports': 0,
        'Entertainment': 0,
        'Lifestyle': 0,
        'Knowledge': 0,
        'Society': 0
    }

    for thread in threads:
        thread.join()

        info = q.get()

        for item in info['items']:
            if 'topicDetails' in item:
                for topic in set(item['topicDetails']['topicIds']):
                    if topic in topic_map:
                        t = topic_map[topic]
                        if len(collections[t][pages[t]]) == 50:
                            collections[t].append({})
                            pages[t] += 1
                        collections[t][pages[t]][item['id']] = None

    user = User.get(username)
    user.update({
        'subscriptions': subs,
        'collections': collections,
        'job_id': None
    })
    print('finished')