def request_token():
    payload = {
        'code': request.args.get('code'),
        'client_id': app.config['CLIENT_ID'],
        'client_secret': app.config['CLIENT_SECRET'],
        'redirect_uri': app.config['LOGIN_TOKEN']
    }
    r = requests.post('https://stackexchange.com/oauth/access_token', data=payload)
    # Convert response to dictionary, since it's text and not json we do this
    qs = r.text
    r = {x.split('=')[0]:x.split('=')[1] for x in qs.split("&")}
    
    SITE = SEAPI.SEAPI('stackoverflow', key=app.config['APP_KEY'], access_token=r['access_token'])
    user_data = SITE.fetch('/me', pagesize=100, filter='!23DDSb5p1Qj1Bj3trg(qA')
    vars = "Content-Type: text/plain\n\n"
    for key, value in user_data['items'][0].iteritems():
        vars += "{} => {} <br/>\n".format(key, value)
    check_user = {}
    check_user['id'] = user_data['items'][0]['account_id']
    check_user['name'] = user_data['items'][0]['display_name']
    check_user['employee'] = user_data['items'][0]['is_employee']
    check_user['creation_date'] = datetime.datetime.fromtimestamp(user_data['items'][0]['creation_date'])
    check_user['id_site'] = user_data['items'][0]['user_id']
    check_user['website'] = user_data['items'][0]['website_url']
    check_user['profile_link'] = user_data['items'][0]['link']
    check_user['token_expires'] = int(r['expires'])
    check_user['access_token'] = r['access_token']
    s = utils.connect_to_db(app)
    import pprint
    pprint.pprint(check_user)
    print r['expires']
    User = utils.get_or_create(s, models.User, **check_user)
    vars += "\n{}".format(User)
    return vars
Пример #2
0
    def add_track(self, variables, audio_file_path):
        filename_in_database = os.path.relpath(audio_file_path, variables.dirs.base_dir)
        if utils.check_if_track_exists(variables, filename_in_database):
            return

        variables.reset_track_data()

        corrupted_file = self.get_tag_data(variables, audio_file_path)
        if corrupted_file == "yes":
            return

        try:
            track_object = variables.network.get_track(variables.track_data['band_name'],
                                                       variables.track_data['song_title'])

            self.get_track_data_from_lastfm(variables)

            if variables.is_band_new:
                try:

                    self.get_band_data_from_lastfm(variables, track_object)

                    session = variables.session()

                    band_instance, new = utils.get_or_create(session, Band,
                                        name=variables.track_data['band_name'],
                                        language='English',
                                        info=variables.track_data['band_info'])

                    variables.add_band(variables.track_data['band_name'],
                                       False,
                                       band_instance.id)

                    session.close()

                    pics.get_band_thumbnail(variables)
                    pics.get_band_cover(variables)

                except Exception as e:
                    print "[-] Caught exception in new band " + str(e)
                    pass

            if variables.is_album_new:
                try:

                    self.get_album_data_from_lastfm(variables, track_object)

                    session = variables.session()

                    album_instance, new = utils.get_or_create(session, Album,
                                        name=variables.track_data['album_name'],
                                        info=variables.track_data['album_info'],
                                        language='English',
                                        band_id=variables.band_id,
                                        band_name=variables.track_data['band_name'])

                    variables.add_album(variables.track_data['album_name'],
                                        False,
                                        album_instance.id)

                    session.close()

                    pics.get_album_thumbnail(variables)

                except AttributeError as e:
                    if str(e) == "'NoneType' object has no attribute 'get_name'":
                        pass
                    else:
                        print "[-] Unknown Exception: %s"% str(e)
                except pylast.WSError as e:
                    # As details from the directory structure will be used
                    if e.details == 'Track not found':
                        pass
                    else:
                        print e.details

                except Exception as e:
                    print "[-] Caught exception in new album " + str(e)
                    pass


        except pylast.WSError as e:
            if str(e) == 'Track not found':
                # Fallback to track
                pass
            else:
                print '[-]pylast Exception:'+str(e)

        except Exception as e:
            # This block is to be looked upon to add exception handling cases
            print '[-]Unkown Exception while fetching track attributes:'+str(e)
            pass

        session = variables.session()

        # If still new create using folder names
        if variables.is_band_new:
            band_instance, new = utils.get_or_create(session, Band,
                                        name=variables.band_name,
                                        language='English',
                                        info=None)
            variables.add_band(variables.band_name,
                               False,
                               band_instance.id)

            pics.get_band_thumbnail(variables)
            pics.get_band_cover(variables)

        if variables.is_album_new:
            album_instance, new = utils.get_or_create(session, Album,
                                        name=variables.album_name,
                                        info=None,
                                        language='English',
                                        band_id=variables.band_id,
                                        band_name=variables.band_name)
            variables.add_album(variables.album_name,
                                False,
                                album_instance.id)

            pics.get_album_thumbnail(variables)


        year_instance, new = utils.get_or_create(session, Year,
                                  name = variables.track_data['year'])

        genre_instance, new = utils.get_or_create(session, Genre,
                                   name = variables.track_data['genre'])

        track, new = utils.get_or_create(session, Track,
                                   file = filename_in_database,
                                   title = variables.track_data['song_title'],
                                   album_id = variables.album_id,
                                   band_id = variables.band_id,
                                   genre_id = genre_instance.id,
                                   artist = variables.track_data['band_name'],
                                   year_id = year_instance.id,
                                   length = variables.track_data['track_duration'],
                                   track = variables.track_data['track_number'])
        session.close()
        album_name = variables.track_data['album_name']\
            if variables.track_data.has_key('album_name') else variables.album_name
        print '[+] %s - %s (%s) added' % (variables.track_data['band_name'],
                                          variables.track_data['song_title'],
                                          album_name)

        self.update_tag_data(variables, audio_file_path)