Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import random
- import json
- import sys
- # Replace these variables with your actual Linkding server URL and API token
- LINKDING_SERVER_URL = "http://10.0.0.101:9090"
- API_TOKEN = "Token"
- # Function to retrieve bookmarks by tag
- def get_bookmarks_by_tag(tag):
- url = f"{LINKDING_SERVER_URL}/api/bookmarks/?tags={tag}"
- headers = {
- "Authorization": f"Token {API_TOKEN}"
- }
- response = requests.get(url, headers=headers)
- if response.status_code == 200:
- bookmarks = response.json().get("results", [])
- return bookmarks
- else:
- print(f"Failed to retrieve bookmarks by tag. Status code: {response.status_code}")
- print(response.text)
- return []
- def filter_bookmarks_by_tag(bookmarks, tag):
- tag = tag.lower()
- filtered_bookmarks = [
- bookmark for bookmark in bookmarks
- if tag in [t.lower() for t in bookmark.get("tag_names", [])]
- ]
- return filtered_bookmarks
- # Function to pick 3 random bookmarks and print their fields
- def print_random_bookmarks(bookmarks, tag, num=3):
- bookmarks = filter_bookmarks_by_tag(bookmarks, tag)
- if len(bookmarks) < num:
- print(f"Less than {num} bookmarks found. Printing all available bookmarks:")
- num = len(bookmarks)
- random_bookmarks = random.sample(bookmarks, num)
- response = {}
- for i, bookmark in enumerate(random_bookmarks):
- response["Title_" + str(i+1)] = bookmark.get("title")
- response["URL_" + str(i+1)] = bookmark.get("url")
- response["Description_" + str(i+1)] = bookmark.get("description", "No description available")
- with open('/config/www/recipe_script_output.json', 'w') as f:
- json.dump(response, f)
- print(json.dumps(response))
- # Main function
- if __name__ == "__main__":
- # Check if the tag is passed as an argument
- if len(sys.argv) > 1:
- tag = sys.argv[1]
- else:
- print("Please provide a tag as a parameter.")
- sys.exit(1)
- bookmarks = get_bookmarks_by_tag(tag)
- if bookmarks:
- print_random_bookmarks(bookmarks, tag)
- else:
- print("No bookmarks found for the specified tag.")
Advertisement
Add Comment
Please, Sign In to add comment