Browse Source

include date of image in filename returned by Bing image provider

master
Niklas Rosenstein 2 years ago
parent
commit
bbc7a1bcea
No known key found for this signature in database GPG Key ID: 6D269B33D25F6C6
  1. 6
      README.md
  2. 16
      src/wpx/__main__.py
  3. 7
      src/wpx/providers/bing.py

6
README.md

@ -1,8 +1,12 @@
# wpx 💻
Wpx is a wallpaper downloader.
Wpx is a wallpaper downloader. That's it.
#### Supported Wallpaper Providers
* [Bing.com](https://www.bing.com/)
* [WallpapersHome](https://wallpapershome.com)
#### Usage
$ FILENAME=$(wpx bing --iotd)

16
src/wpx/__main__.py

@ -25,7 +25,10 @@ from typing import Optional, BinaryIO
from wpx.providers import IImageProvider
import argparse
import json
import logging
import os
logger = logging.getLogger(__name__)
mapper = ObjectMapper(JsonModule())
@ -45,11 +48,14 @@ def get_argument_parser(prog: Optional[str]):
parser.add_argument('provider', help='The name of the image provider.')
parser.add_argument('-c', '--config', help='Inline JSON configuration of the provider.')
parser.add_argument('-d', '--directory', help='The output directory.')
parser.add_argument('--overwrite', action='store_true', help='Overwrite output file(s).')
parser.add_argument('--iotd', action='store_true', help='Get the image of the day.')
return parser
def main(argv=None, prog=None):
logging.basicConfig(format='%(message)s', level=logging.INFO)
parser = get_argument_parser(prog)
args = parser.parse_args(argv)
@ -65,9 +71,13 @@ def main(argv=None, prog=None):
if args.iotd:
image = provider.get_image_of_the_day()
print(image.filename)
with image.open() as src:
save_to(image.filename, src)
if args.overwrite or not os.path.isfile(image.filename):
with image.open() as src:
save_to(image.filename, src)
else:
logging.info('note: "%s" already exists', image.filename)
# Print the absolute path to the saved file so that scripts can get it.
print(os.path.abspath(image.filename))
_entry_main = lambda: exit(main())

7
src/wpx/providers/bing.py

@ -25,6 +25,7 @@ from nr.databind.core import (
Field,
MixinDecoration,
Struct)
from nr.databind.json import JsonFieldName
from nr.interface import implements
from typing import Iterable, List, Tuple
from wpx.providers import IImage, IImageProvider
@ -138,6 +139,7 @@ class BingImageClient:
@implements(IImageProvider)
class BingImageProvider(Struct):
market = Field(str, default='EN-IN')
date_in_filename = Field(bool, JsonFieldName('dateInFilename'), default=True)
def __init__(self, *args, **kwargs):
super(BingImageProvider, self).__init__(*args, **kwargs)
@ -145,4 +147,7 @@ class BingImageProvider(Struct):
def get_image_of_the_day(self) -> IImage:
image = self.client.get_image()
return IImage(filename=image.get_filename(), open=image.open)
filename = image.get_filename()
if self.date_in_filename:
filename = image.startdate.strftime('%Y-%m-%d-') + filename
return IImage(filename=filename, open=image.open)

Loading…
Cancel
Save