コード例 #1
0
def get_flickr(config_file):
	config = ConfigParser.ConfigParser()
	config.read(config_file)

	api_key = config.get('Flickr', 'api_key')
	api_secret = config.get('Flickr', 'api_secret')
	api = FlickrAPI(api_key, api_secret, cache=True)
	if not api.token_valid(perms=u'read'):
                            api.get_request_token(oauth_callback='oob')
                            authorize_url = api.auth_url(perms=u'read')
                            verifier = unicode(raw_input('Verifier code for %s:' % authorize_url))
                            api.get_access_token(verifier)
	return api
コード例 #2
0
def main():
    """ Main, for running from command line """

    parser = argparse.ArgumentParser(
        description='Keep My Photos for backup of flickr photos')
    parser.add_argument(
        '-u',
        '--url',
        help=
        'The photos URL of the user eg. http://www.flickr.com/photos/keepmyphotos/'
    )
    parser.add_argument('-d',
                        '--dir',
                        help='The directory where the backup resides',
                        required=True)
    parser.add_argument('-i',
                        '--id',
                        help='The flickr user ID eg 12345678@N01')
    parser.add_argument('-o',
                        '--original-only',
                        help='Fail if original photos are not available',
                        action='store_true')
    parser.add_argument('-N',
                        '--not-in-set-only',
                        help='Process "not in set" pictures only.',
                        action='store_true')
    args = parser.parse_args()

    if args.url is not None:
        args.id = KeepMyPhotos.find_user_id(args.url)

    if args.id is None:
        print(colorama.Fore.RED +
              "Please specify -u <photostream url> OR -i user@id")
    else:
        # get api
        flickr = FlickrAPI(api_key, api_secret)

        # init authentication for the flickr api.
        if not flickr.token_valid(perms=api_access_level):
            flickr.get_request_token(oauth_callback='oob')
            authorize_url = flickr.auth_url(perms=api_access_level)
            print("Open authorize_url: %s" % (authorize_url, ))
            verifier = input('Verifier code: ')
            flickr.get_access_token(verifier)

        kmp = KeepMyPhotos(flickr, args)
        kmp.backup_flickr_all(args.id)
コード例 #3
0

print('Creating FlickrAPI object')

flickr = FlickrAPI(keys.apikey, keys.apisecret)

# ------------------------------------------------------------------------------
print('Step 1: authenticate')

if not flickr.token_valid(perms='read'):
    # Get a request token
    flickr.get_request_token(oauth_callback='oob')

    # Open a browser at the authentication URL. Do this however
    # you want, as long as the user visits that URL.
    authorize_url = flickr.auth_url(perms='read')
    webbrowser.open_new_tab(authorize_url)

    # Get the verifier code from the user. Do this however you
    # want, as long as the user gives the application the code.
    verifier = six.text_type(six.input('Verifier code: '))

    # Trade the request token for an access token
    flickr.get_access_token(verifier)

# ------------------------------------------------------------------------------
print('Step 2: use Flickr')
resp = flickr.photos.getInfo(photo_id='7658567128')

ET.dump(resp)
コード例 #4
0
    apisecret = u'03fbb3ea705fe096'

print('Creating FlickrAPI object')

flickr = FlickrAPI(keys.apikey, keys.apisecret)

# ------------------------------------------------------------------------------
print('Step 1: authenticate')

if not flickr.token_valid(perms='read'):
    # Get a request token
    flickr.get_request_token(oauth_callback='oob')
    
    # Open a browser at the authentication URL. Do this however
    # you want, as long as the user visits that URL.
    authorize_url = flickr.auth_url(perms='read')
    webbrowser.open_new_tab(authorize_url)
    
    # Get the verifier code from the user. Do this however you
    # want, as long as the user gives the application the code.
    verifier = unicode(raw_input('Verifier code: '))
    
    # Trade the request token for an access token
    flickr.get_access_token(verifier)

# ------------------------------------------------------------------------------
print('Step 2: use Flickr')
resp = flickr.photos.getInfo(photo_id='7658567128')

ET.dump(resp)
コード例 #5
0
class iesPicture():
    def __init__(self, pageNum=50):

        self.flickr = FlickrAPI(FLICKRKEY.encode('utf-8'),
                                FLICKRSECRET,
                                format='parsed-json')

        if not self.flickr.token_valid():
            self.flickr.get_request_token(oauth_callback="oob")

            verifier = str(
                input(
                    "Get verifier code from {} and enter it here.\n: ".format(
                        self.flickr.auth_url(perms="write"))))

            self.flickr.get_access_token(verifier)

        self.LCD = lcd1602.LCD16021()

    def choosePrintPicture(self, pageNum=50):
        photos = self.flickr.photos_search(user_id=MYID, per_page=pageNum)

        urltitledict = {}

        for i in range(pageNum):
            photoData = photos['photos']['photo'][i]

            farmId = photoData['farm']
            severId = photoData['server']
            picId = photoData['id']
            idSecreat = photoData['secret']
            title = photoData["title"]

            url = "https://farm{0}.staticflickr.com/{1}/{2}_{3}.jpg".format(
                farmId, severId, picId, idSecreat)

            urltitledict[url] = title

        dlURL, title = random.choice(list(urltitledict.items()))

        print(dlURL)

        self.LCD.lcd_string("Title", 0x80)
        self.LCD.lcd_string(title, 0xC0)

        self.downloadPicture(dlURL, "./Image/downloadpic.jpg")
        self.pictureConvert("./Image/downloadpic.jpg")

    def downloadPicture(self, url, filename):
        r = requests.get(url, stream=True)
        if r.status_code == 200:
            with open(filename, 'wb') as f:
                r.raw.decode_content = True
                shutil.copyfileobj(r.raw, f)

    def pictureConvert(self, name):
        root, ext = os.path.splitext(name)
        newext = ".png"
        args = ['convert', name, root + newext]
        res = subprocess.check_call(args)
        print(res)

    def uploadPhoto(self, path_to_photo, title=None):
        """ Upload the photo file to flickr.
        arguments:
            api: FlickrAPI object.
            path_to_photo: Path to photo file.
            title: photo file's name if None.
        returns:
            uploaded photo ID if success, else None.
        """

        # res is instance of xml.etree.ElementTree.Element.
        # This element has something like "<rsp><photoid>1234</photoid></rsp>".
        """
        if not api.token_valid():
            api.get_request_token(oauth_callback="oob")

            verifier = str(input("Get verifier code from {} and enter it here.\n: ".format(api.auth_url(perms="write"))))

            api.get_access_token(verifier)
        """

        try:
            res = self.flickr.upload(
                filename=path_to_photo,
                title=os.path.basename(path_to_photo) if not title else title,
                is_private=True)

        except json.decoder.JSONDecodeError:
            return None

        if res.get("stat") != "ok":
            return None

        # Get the uploaded photo's ID.
        return res