Remove now-unneeded Tristate

This commit is contained in:
Alexander Graf 2018-04-13 19:06:26 +02:00
parent 7976a4811e
commit df1cdb5d48
6 changed files with 27 additions and 69 deletions

View File

@ -96,8 +96,6 @@ Miscellaneous Functions
.. autofunction:: mediaid_to_shortcode .. autofunction:: mediaid_to_shortcode
.. autoclass:: Tristate
Exceptions Exceptions
^^^^^^^^^^ ^^^^^^^^^^

View File

@ -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 request to the Instagram server for each picture, which is why it is disabled
by default. by default.
.. option:: --no-geotags
Do not store geotags, even if they can be obtained without any additional
request.
.. option:: --comments .. option:: --comments
Download and update comments for each post. This requires an additional Download and update comments for each post. This requires an additional

View File

@ -13,6 +13,6 @@ else:
win_unicode_console.enable() win_unicode_console.enable()
from .exceptions import * 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, from .structures import (Post, Profile, Story, StoryItem, load_structure_from_file, mediaid_to_shortcode,
save_structure_to_file, shortcode_to_mediaid) save_structure_to_file, shortcode_to_mediaid)

View File

@ -7,7 +7,7 @@ from argparse import ArgumentParser, SUPPRESS
from typing import Callable, List, Optional from typing import Callable, List, Optional
from . import (Instaloader, InstaloaderException, InvalidArgumentException, Post, Profile, ProfileNotExistsException, 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 .instaloader import get_default_session_filename
from .instaloadercontext import default_user_agent from .instaloadercontext import default_user_agent
@ -189,8 +189,6 @@ def main():
'text file with the location\'s name and a Google Maps link. ' 'text file with the location\'s name and a Google Maps link. '
'This requires an additional request to the Instagram ' 'This requires an additional request to the Instagram '
'server for each picture, which is why it is disabled by default.') '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', g_what.add_argument('-C', '--comments', action='store_true',
help='Download and update comments for each post. ' help='Download and update comments for each post. '
'This requires an additional request to the Instagram ' '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 " raise SystemExit(":feed-all and :feed-liked were removed. Use :feed as target and "
"eventually --only-if=viewer_has_liked.") "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, loader = Instaloader(sleep=not args.no_sleep, quiet=args.quiet,
user_agent=args.user_agent, user_agent=args.user_agent,
dirname_pattern=args.dirname_pattern, filename_pattern=args.filename_pattern, dirname_pattern=args.dirname_pattern, filename_pattern=args.filename_pattern,
download_videos=download_videos, download_video_thumbnails=download_video_thumbnails, download_videos=not args.no_videos, download_video_thumbnails=not args.no_video_thumbnails,
download_geotags=download_geotags, download_geotags=args.geotags,
save_captions=save_captions, download_comments=download_comments, save_captions=not args.no_captions, download_comments=args.comments,
save_metadata=save_metadata, compress_json=not args.no_compress_json, save_metadata=not args.no_metadata_json, compress_json=not args.no_compress_json,
max_connection_attempts=args.max_connection_attempts) max_connection_attempts=args.max_connection_attempts)
_main(loader, _main(loader,
args.profile, args.profile,

View File

@ -9,7 +9,6 @@ import sys
import tempfile import tempfile
from contextlib import contextmanager, suppress from contextlib import contextmanager, suppress
from datetime import datetime from datetime import datetime
from enum import Enum
from functools import wraps from functools import wraps
from io import BytesIO from io import BytesIO
from typing import Any, Callable, Dict, Iterator, List, Optional 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 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: def _requires_login(func: Callable) -> Callable:
"""Decorator to raise an exception if herewith-decorated function is called without being logged in""" """Decorator to raise an exception if herewith-decorated function is called without being logged in"""
@wraps(func) @wraps(func)
@ -78,12 +60,12 @@ class Instaloader:
user_agent: Optional[str] = None, user_agent: Optional[str] = None,
dirname_pattern: Optional[str] = None, dirname_pattern: Optional[str] = None,
filename_pattern: Optional[str] = None, filename_pattern: Optional[str] = None,
download_videos: Tristate = Tristate.always, download_videos: bool = True,
download_video_thumbnails: Tristate = Tristate.always, download_video_thumbnails: bool = True,
download_geotags: Tristate = Tristate.no_extra_query, download_geotags: bool = True,
save_captions: Tristate = Tristate.no_extra_query, save_captions: bool = True,
download_comments: Tristate = Tristate.no_extra_query, download_comments: bool = True,
save_metadata: Tristate = Tristate.no_extra_query, save_metadata: bool = True,
compress_json: bool = True, compress_json: bool = True,
max_connection_attempts: int = 3): max_connection_attempts: int = 3):
@ -352,14 +334,14 @@ class Instaloader:
edge_number = 1 edge_number = 1
for edge in post.get_sidecar_edges(): for edge in post.get_sidecar_edges():
# Download picture or video thumbnail # 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, downloaded |= self.download_pic(filename=filename,
filename_alt=filename_old, filename_alt=filename_old,
url=edge['node']['display_url'], url=edge['node']['display_url'],
mtime=post.date_local, mtime=post.date_local,
filename_suffix=str(edge_number)) filename_suffix=str(edge_number))
# Additionally download video if available and desired # 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, downloaded |= self.download_pic(filename=filename,
filename_alt=filename_old, filename_alt=filename_old,
url=edge['node']['video_url'], url=edge['node']['video_url'],
@ -370,14 +352,14 @@ class Instaloader:
downloaded = self.download_pic(filename=filename, filename_alt=filename_old, downloaded = self.download_pic(filename=filename, filename_alt=filename_old,
url=post.url, mtime=post.date_local) url=post.url, mtime=post.date_local)
elif post.typename == 'GraphVideo': 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, downloaded = self.download_pic(filename=filename, filename_alt=filename_old,
url=post.url, mtime=post.date_local) url=post.url, mtime=post.date_local)
else: else:
self.context.error("Warning: {0} has unknown typename: {1}".format(post, post.typename)) self.context.error("Warning: {0} has unknown typename: {1}".format(post, post.typename))
# Save caption if desired # Save caption if desired
if self.save_captions is not Tristate.never: if self.save_captions is not False:
if post.caption: if post.caption:
self.save_caption(filename=filename, filename_alt=filename_old, self.save_caption(filename=filename, filename_alt=filename_old,
mtime=post.date_local, caption=post.caption) mtime=post.date_local, caption=post.caption)
@ -385,22 +367,22 @@ class Instaloader:
self.context.log("<no caption>", end=' ', flush=True) self.context.log("<no caption>", end=' ', flush=True)
# Download video if desired # 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, downloaded |= self.download_pic(filename=filename, filename_alt=filename_old,
url=post.video_url, mtime=post.date_local) url=post.video_url, mtime=post.date_local)
# Download geotags if desired # Download geotags if desired
if self.download_geotags is Tristate.always: if self.download_geotags is True:
location = post.get_location() location = post.get_location()
if location: if location:
self.save_location(filename, location, post.date_local) self.save_location(filename, location, post.date_local)
# Update comments if desired # 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) self.update_comments(filename=filename, filename_alt=filename_old, post=post)
# Save metadata as JSON if desired. # 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.save_metadata_json(filename, post)
self.context.log() self.context.log()
@ -483,19 +465,19 @@ class Instaloader:
shortcode=shortcode) shortcode=shortcode)
os.makedirs(os.path.dirname(filename), exist_ok=True) os.makedirs(os.path.dirname(filename), exist_ok=True)
downloaded = False 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 url = item.url
downloaded = self.download_pic(filename=filename, downloaded = self.download_pic(filename=filename,
filename_alt=filename_old, filename_alt=filename_old,
url=url, url=url,
mtime=date_local) 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, downloaded |= self.download_pic(filename=filename,
filename_alt=filename_old, filename_alt=filename_old,
url=item.video_url, url=item.video_url,
mtime=date_local) mtime=date_local)
# Save metadata as JSON if desired. # 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.save_metadata_json(filename, item)
self.context.log() self.context.log()
return downloaded return downloaded
@ -707,7 +689,7 @@ class Instaloader:
profile_name = profile.username profile_name = profile.username
# Save metadata as JSON if desired. # 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), json_filename = '{0}/{1}_{2}'.format(self.dirname_pattern.format(profile=profile_name, target=profile_name),
profile_name, profile.userid) profile_name, profile.userid)
self.save_metadata_json(json_filename, profile) self.save_metadata_json(json_filename, profile)

View File

@ -22,9 +22,9 @@ class TestInstaloader(unittest.TestCase):
self.dir = tempfile.mkdtemp() self.dir = tempfile.mkdtemp()
print("Testing in {}".format(self.dir)) print("Testing in {}".format(self.dir))
os.chdir(self.dir) os.chdir(self.dir)
self.L = instaloader.Instaloader(download_geotags=instaloader.Tristate.always, self.L = instaloader.Instaloader(download_geotags=True,
download_comments=instaloader.Tristate.always, download_comments=True,
save_metadata=instaloader.Tristate.always) save_metadata=True)
self.L.context.raise_all_errors = True self.L.context.raise_all_errors = True
def tearDown(self): def tearDown(self):