From df1cdb5d4861345f87ac7863b6ccbfdb6c9a6c14 Mon Sep 17 00:00:00 2001 From: Alexander Graf Date: Fri, 13 Apr 2018 19:06:26 +0200 Subject: [PATCH] Remove now-unneeded Tristate --- docs/as-module.rst | 2 -- docs/cli-options.rst | 5 ---- instaloader/__init__.py | 2 +- instaloader/__main__.py | 27 ++++-------------- instaloader/instaloader.py | 54 ++++++++++++----------------------- test/instaloader_unittests.py | 6 ++-- 6 files changed, 27 insertions(+), 69 deletions(-) diff --git a/docs/as-module.rst b/docs/as-module.rst index 4206817..e64f6d7 100644 --- a/docs/as-module.rst +++ b/docs/as-module.rst @@ -96,8 +96,6 @@ Miscellaneous Functions .. autofunction:: mediaid_to_shortcode -.. autoclass:: Tristate - Exceptions ^^^^^^^^^^ diff --git a/docs/cli-options.rst b/docs/cli-options.rst index 5120465..d3721a4 100644 --- a/docs/cli-options.rst +++ b/docs/cli-options.rst @@ -46,11 +46,6 @@ automatically **finds it by its unique ID** and renames the folder likewise. request to the Instagram server for each picture, which is why it is disabled by default. -.. option:: --no-geotags - - Do not store geotags, even if they can be obtained without any additional - request. - .. option:: --comments Download and update comments for each post. This requires an additional diff --git a/instaloader/__init__.py b/instaloader/__init__.py index 20fcc74..c9dd4f8 100644 --- a/instaloader/__init__.py +++ b/instaloader/__init__.py @@ -13,6 +13,6 @@ else: win_unicode_console.enable() from .exceptions import * -from .instaloader import Instaloader, Tristate +from .instaloader import Instaloader from .structures import (Post, Profile, Story, StoryItem, load_structure_from_file, mediaid_to_shortcode, save_structure_to_file, shortcode_to_mediaid) diff --git a/instaloader/__main__.py b/instaloader/__main__.py index 6843efb..70073ee 100644 --- a/instaloader/__main__.py +++ b/instaloader/__main__.py @@ -7,7 +7,7 @@ from argparse import ArgumentParser, SUPPRESS from typing import Callable, List, Optional from . import (Instaloader, InstaloaderException, InvalidArgumentException, Post, Profile, ProfileNotExistsException, - StoryItem, Tristate, __version__, load_structure_from_file) + StoryItem, __version__, load_structure_from_file) from .instaloader import get_default_session_filename from .instaloadercontext import default_user_agent @@ -189,8 +189,6 @@ def main(): 'text file with the location\'s name and a Google Maps link. ' 'This requires an additional request to the Instagram ' 'server for each picture, which is why it is disabled by default.') - g_what.add_argument('--no-geotags', action='store_true', - help='Do not store geotags, even if they can be obtained without any additional request.') g_what.add_argument('-C', '--comments', action='store_true', help='Download and update comments for each post. ' 'This requires an additional request to the Instagram ' @@ -279,28 +277,13 @@ def main(): raise SystemExit(":feed-all and :feed-liked were removed. Use :feed as target and " "eventually --only-if=viewer_has_liked.") - download_videos = Tristate.always if not args.no_videos else Tristate.no_extra_query - download_video_thumbnails = Tristate.always if not args.no_video_thumbnails else Tristate.never - download_comments = Tristate.always if args.comments else Tristate.no_extra_query - save_captions = Tristate.no_extra_query if not args.no_captions else Tristate.never - save_metadata = Tristate.always if not args.no_metadata_json else Tristate.never - - if args.geotags and args.no_geotags: - raise SystemExit("--geotags and --no-geotags given. I am confused and refuse to work.") - elif args.geotags: - download_geotags = Tristate.always - elif args.no_geotags: - download_geotags = Tristate.never - else: - download_geotags = Tristate.no_extra_query - loader = Instaloader(sleep=not args.no_sleep, quiet=args.quiet, user_agent=args.user_agent, dirname_pattern=args.dirname_pattern, filename_pattern=args.filename_pattern, - download_videos=download_videos, download_video_thumbnails=download_video_thumbnails, - download_geotags=download_geotags, - save_captions=save_captions, download_comments=download_comments, - save_metadata=save_metadata, compress_json=not args.no_compress_json, + download_videos=not args.no_videos, download_video_thumbnails=not args.no_video_thumbnails, + download_geotags=args.geotags, + save_captions=not args.no_captions, download_comments=args.comments, + save_metadata=not args.no_metadata_json, compress_json=not args.no_compress_json, max_connection_attempts=args.max_connection_attempts) _main(loader, args.profile, diff --git a/instaloader/instaloader.py b/instaloader/instaloader.py index d11e0d3..810a3fa 100644 --- a/instaloader/instaloader.py +++ b/instaloader/instaloader.py @@ -9,7 +9,6 @@ import sys import tempfile from contextlib import contextmanager, suppress from datetime import datetime -from enum import Enum from functools import wraps from io import BytesIO from typing import Any, Callable, Dict, Iterator, List, Optional @@ -34,23 +33,6 @@ def format_string_contains_key(format_string: '_PathPattern', key: str) -> bool: return False -class Tristate(Enum): - """Tri-state to encode whether we should save certain information, i.e. videos, captions, comments or geotags. - - :attr:`never` - Do not save, even if the information is available without any additional request, - - :attr:`no_extra_query` - Save if and only if available without doing additional queries, - - :attr:`always` - Save (and query, if neccessary). - """ - never = 0 - no_extra_query = 1 - always = 2 - - def _requires_login(func: Callable) -> Callable: """Decorator to raise an exception if herewith-decorated function is called without being logged in""" @wraps(func) @@ -78,12 +60,12 @@ class Instaloader: user_agent: Optional[str] = None, dirname_pattern: Optional[str] = None, filename_pattern: Optional[str] = None, - download_videos: Tristate = Tristate.always, - download_video_thumbnails: Tristate = Tristate.always, - download_geotags: Tristate = Tristate.no_extra_query, - save_captions: Tristate = Tristate.no_extra_query, - download_comments: Tristate = Tristate.no_extra_query, - save_metadata: Tristate = Tristate.no_extra_query, + download_videos: bool = True, + download_video_thumbnails: bool = True, + download_geotags: bool = True, + save_captions: bool = True, + download_comments: bool = True, + save_metadata: bool = True, compress_json: bool = True, max_connection_attempts: int = 3): @@ -352,14 +334,14 @@ class Instaloader: edge_number = 1 for edge in post.get_sidecar_edges(): # Download picture or video thumbnail - if not edge['node']['is_video'] or self.download_video_thumbnails is Tristate.always: + if not edge['node']['is_video'] or self.download_video_thumbnails is True: downloaded |= self.download_pic(filename=filename, filename_alt=filename_old, url=edge['node']['display_url'], mtime=post.date_local, filename_suffix=str(edge_number)) # Additionally download video if available and desired - if edge['node']['is_video'] and self.download_videos is Tristate.always: + if edge['node']['is_video'] and self.download_videos is True: downloaded |= self.download_pic(filename=filename, filename_alt=filename_old, url=edge['node']['video_url'], @@ -370,14 +352,14 @@ class Instaloader: downloaded = self.download_pic(filename=filename, filename_alt=filename_old, url=post.url, mtime=post.date_local) elif post.typename == 'GraphVideo': - if self.download_video_thumbnails is Tristate.always: + if self.download_video_thumbnails is True: downloaded = self.download_pic(filename=filename, filename_alt=filename_old, url=post.url, mtime=post.date_local) else: self.context.error("Warning: {0} has unknown typename: {1}".format(post, post.typename)) # Save caption if desired - if self.save_captions is not Tristate.never: + if self.save_captions is not False: if post.caption: self.save_caption(filename=filename, filename_alt=filename_old, mtime=post.date_local, caption=post.caption) @@ -385,22 +367,22 @@ class Instaloader: self.context.log("", end=' ', flush=True) # Download video if desired - if post.is_video and self.download_videos is Tristate.always: + if post.is_video and self.download_videos is True: downloaded |= self.download_pic(filename=filename, filename_alt=filename_old, url=post.video_url, mtime=post.date_local) # Download geotags if desired - if self.download_geotags is Tristate.always: + if self.download_geotags is True: location = post.get_location() if location: self.save_location(filename, location, post.date_local) # Update comments if desired - if self.download_comments is Tristate.always: + if self.download_comments is True: self.update_comments(filename=filename, filename_alt=filename_old, post=post) # Save metadata as JSON if desired. - if self.save_metadata is not Tristate.never: + if self.save_metadata is not False: self.save_metadata_json(filename, post) self.context.log() @@ -483,19 +465,19 @@ class Instaloader: shortcode=shortcode) os.makedirs(os.path.dirname(filename), exist_ok=True) downloaded = False - if not item.is_video or self.download_video_thumbnails is Tristate.always: + if not item.is_video or self.download_video_thumbnails is True: url = item.url downloaded = self.download_pic(filename=filename, filename_alt=filename_old, url=url, mtime=date_local) - if item.is_video and self.download_videos is Tristate.always: + if item.is_video and self.download_videos is True: downloaded |= self.download_pic(filename=filename, filename_alt=filename_old, url=item.video_url, mtime=date_local) # Save metadata as JSON if desired. - if self.save_metadata is not Tristate.never: + if self.save_metadata is not False: self.save_metadata_json(filename, item) self.context.log() return downloaded @@ -707,7 +689,7 @@ class Instaloader: profile_name = profile.username # Save metadata as JSON if desired. - if self.save_metadata is not Tristate.never: + if self.save_metadata is not False: json_filename = '{0}/{1}_{2}'.format(self.dirname_pattern.format(profile=profile_name, target=profile_name), profile_name, profile.userid) self.save_metadata_json(json_filename, profile) diff --git a/test/instaloader_unittests.py b/test/instaloader_unittests.py index 63772ba..7cd8a1e 100644 --- a/test/instaloader_unittests.py +++ b/test/instaloader_unittests.py @@ -22,9 +22,9 @@ class TestInstaloader(unittest.TestCase): self.dir = tempfile.mkdtemp() print("Testing in {}".format(self.dir)) os.chdir(self.dir) - self.L = instaloader.Instaloader(download_geotags=instaloader.Tristate.always, - download_comments=instaloader.Tristate.always, - save_metadata=instaloader.Tristate.always) + self.L = instaloader.Instaloader(download_geotags=True, + download_comments=True, + save_metadata=True) self.L.context.raise_all_errors = True def tearDown(self):