Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #43 from hughrun/310b
Browse files Browse the repository at this point in the history
310b
  • Loading branch information
hughrun authored Feb 14, 2021
2 parents 959ba52 + 582e774 commit 69c3aad
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 40 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This is the version 3.x documentation. If you prefer to use an older version you

## tl;dr

1. make sure you have installed Python version 3.x (preferably 3.7 or higher)
1. make sure you have installed Python version 3.6.0 or higher.
2. `pip install pocketsnack` (you may need to use `pip3` instead)
3. `pocketsnack --config`
4. Add your Pocket API consumer key to the config file
Expand Down Expand Up @@ -35,6 +35,7 @@ You can adjust most settings, but the defaults should be sensible for most users
| archive_tag | string | the tag to use to identify items in your 'to be read' archive|
| ignore_tags | list | a list of tag names - items with any of these tags will be ignored by `--stash` and remain in your Pocket List|
| ignore_faves | boolean | if set to `true` favorited items will be ignored by `--stash` and remain in your Pocket List|
| fave_dupes | boolean | if set to `true` the remaining (original) item will be favorited when duplicates are removed with `--dedupe`|
| replace_all_tags | boolean | if set to `true` all tags will be removed by `--stash` when adding the `archive_tag`, except anything in `retain_tags`|
| retain_tags | list | a list of tag names - these tags will not be removed by `--purge`, nor by `--stash` if `replace_all_tags` is set to `true`|
| longreads_wordcount | integer | determines how long a 'longread' is. |
Expand Down
18 changes: 14 additions & 4 deletions pocketsnack/pocketsnack.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ def main():
if vars(options)[x]:
true_vars.append(x)

if S['pocket_consumer_key'] == 'YOUR_KEY_HERE':
print(' ⚠️ You have not set a pocket_consumer_key in your configuration file. Run \033[46;97mpocketsnack --config\033[0;m or check the README for help.')

if options.config:
conf = pt.config()
print(conf)
Expand All @@ -69,7 +72,7 @@ def main():

location = tag if tag else state if state != 'unread' else 'list'
print(' \033[46;97mChecking for duplicates in ' + location + '\033[0;m')
pt.dedupe(state, tag, consumer_key, access_token)
pt.dedupe(state, tag, S['fave_dupes'], consumer_key, access_token)

elif options.lucky_dip:
print(' \033[46;97mRunning lucky dip...\033[0;m')
Expand Down Expand Up @@ -216,6 +219,9 @@ def print_info(response, collection):
# we do nothing here
pass

except ValueError:
print(" 😳 Whoops, look's like there is a problem with your config file. Try \033[46;97mpocketsnack --config\033[0;m to fix this")

# -----------------------------------
# Parse commands (the action is here)
# -----------------------------------
Expand Down Expand Up @@ -258,7 +264,7 @@ def print_info(response, collection):
"-d", "--lucky_dip", action="store_true", help="move random items tagged 'tbr' from archive to list, depending on config"
)
actions.add_argument(
"--dedupe", action="store_true", help="de-duplicate list (-l), archive (-a), tbr items (--tbr) or all (-b)- defaults to list"
"--dedupe", action="store_true", help="de-duplicate list (-l), archive (-a), tbr items (--tbr) or all (-b). Defaults to list"
)
actions.add_argument(
"-i", "--info", action="store_true", help="get information on items in list or TBR items in archive"
Expand Down Expand Up @@ -299,5 +305,9 @@ def print_info(response, collection):

except FileNotFoundError:
print(' \033[46;97mpocketsnack\033[0;m needs a config file!')
conf = pt.config()
print(conf)
user_input = input(' Do you want to create one now using your default text editor (y/n)?')
if user_input in ["y", "yes", "Y", "Yes", "YES"]:
conf = pt.config()
print(conf)
else:
print(' Some other time then.')
43 changes: 35 additions & 8 deletions pocketsnack/toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def config():
new_config.write('ignore_tags: \n')
new_config.write(' - ignore\n')
new_config.write('ignore_faves: true\n')
new_config.write('fave_dupes: true\n')
new_config.write('replace_all_tags: false\n')
new_config.write('retain_tags:\n')
new_config.write(' - glam blog club\n')
Expand Down Expand Up @@ -172,7 +173,7 @@ def config():
else:
subprocess.call(["xdg-open", config_file])

return 'Config file opened for editing.'
return ' Config file opened for editing'

# ----------------
# Authorise
Expand Down Expand Up @@ -607,7 +608,7 @@ def test(consumer_key, pocket_access_token):
# de-duplicate
# -----------------

def dedupe(state, tag, consumer_key, pocket_access_token):
def dedupe(state, tag, fave_dupes, consumer_key, pocket_access_token):

# Retrieving items
# ----------------
Expand Down Expand Up @@ -635,6 +636,8 @@ def dedupe(state, tag, consumer_key, pocket_access_token):

# and make a list called 'items_to_delete'
items_to_delete = []
# the originals will be faved if the config file says to do that
items_to_fave = []

# loop over each key (not the whole object) in item_list
# 'item' here refers to each item's key, not the whole object/dictionary
Expand Down Expand Up @@ -675,6 +678,12 @@ def dedupe(state, tag, consumer_key, pocket_access_token):
print(' \033[46;97m' + item + '\033[0;m occurs ' + str(len(summary[item])) + ' times')
# keep only the most recently added item by slicing the list to make a new list of everything except the last one (which will be the *first* one that was found)
duplicates = summary[item][:-1]

# TODO: optionally fave the dupe we're keeping
if fave_dupes:
# get last item (which we are keeping) and add to faving list
items_to_fave.append(summary[item][-1])

# add each duplicate in the duplicates list for this url to the items_to_delete list
for item in duplicates:
items_to_delete.append(item)
Expand All @@ -687,21 +696,39 @@ def dedupe(state, tag, consumer_key, pocket_access_token):

# With our list of duplicate item ids, we create one final list of a bunch of JSON objects
actions = []

faves = []
# for each item to be deleted, append a dictionary to the actions list
for item_id in items_to_delete:
actions.append({"action":"delete", "item_id": item_id})

for item_id in items_to_fave:
faves.append({"action":"favorite", "item_id": item_id})

# Turn the lists and component dictionaries into JSON strings
actions_string = json.dumps(actions)
faves_string = json.dumps(faves)
# Now URL encode it using urllib
actions_escaped = urllib.parse.quote(actions_string)
faves_escaped = urllib.parse.quote(faves_string)
# Double check you really want to delete them
if len(actions) > 0:
print(' \033[107;95mAbout to delete ' + str(len(actions)) + ' duplicate items.\033[0;m')
if fave_dupes:
print(' \033[107;95mAbout to delete ' + str(len(actions)) + ' duplicate items and favorite the originals.\033[0;m')
else:
print(' \033[107;95mAbout to delete ' + str(len(actions)) + ' duplicate items.\033[0;m')
print(' \033[107;95mDelete these items? Type "delete" to confirm.\033[0;m')
check = input('>>')
if check == 'delete':
# first turn the list and its component dictionaries into a JSON string
actions_string = json.dumps(actions)
# now URL encode it using urllib
actions_escaped = urllib.parse.quote(actions_string)

if fave_dupes:
# fave remaining
faved = send(faves_escaped, consumer_key, pocket_access_token)
if str(faved) == '<Response [200]>':
print(' \033[46;97mRemaining duplicates faved...\033[0;m')
else:
print(' \033[46;97mSomething went wrong favoriting your dupes 😟\033[0;m')
print(faved.data)

# now POST to pocket and assign the response to a parameter at the same time.
deleted = send(actions_escaped, consumer_key, pocket_access_token)
# provide feedback on what happened
Expand Down
50 changes: 25 additions & 25 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pocketsnack"
version = "3.1.0a"
version = "3.1.0b0"
description = "KonMari your Pocket tsundoku from the command line"
authors = ["Hugh Rundle <hugh@hughrundle.net>"]
license = "GPL-3.0-or-later"
Expand All @@ -18,7 +18,7 @@ include = [
]

[tool.poetry.dependencies]
python = "^3.7"
python = "^3.6"
requests = "^2.25.1"
argparse = "^1.4.0"
pyyaml = "^5.4.1"
Expand Down

0 comments on commit 69c3aad

Please sign in to comment.