Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug Fix and Tag options #22

Merged
merged 3 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ LINKWARDEN_TOKEN=your_linkwarden_api_token_here
#### FOR CRONTAB: ####
COLLECTION_ID=30
CRON_SCHEDULE="0 6 * * *" # Cron Schedule (default is daily at 6am)

#### Tagging Options: ####
OPT_TAG=true # Enable/Disable all Tagging
OPT_TAG_GITHUB=true # Tag Link with "GitHub"
OPT_TAG_GITHUBSTARS=true # Tag Link with "GitHub Stars"
OPT_TAG_LANGUAGE=false # Tag Link with Language of repo (e.g. Python or JavaScript)
OPT_TAG_USERNAME=false # Tag GitHub username
OPT_TAG_CUSTOM=false # Add custom tags, separated by commas (e.g. tag1,tag2)
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ ENV LINKWARDEN_TOKEN=""
ENV COLLECTION_ID=""
ENV CRON_SCHEDULE="0 6 * * *"

ENV OPT_TAG=true
ENV OPT_TAG_GITHUB=true
ENV OPT_TAG_GITHUBSTARS=true
ENV OPT_TAG_LANGUAGE=false
ENV OPT_TAG_USERNAME=false
ENV OPT_TAG_CUSTOM=false

WORKDIR /app

RUN apt-get update && apt-get install -y cron && rm -rf /var/lib/apt/lists/*
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ StarWarden allows you to export GitHub stars to Linkwarden.
GITHUB_USERNAME=your_github_username
LINKWARDEN_URL=your_linkwarden_instance_url
LINKWARDEN_TOKEN=your_linkwarden_api_token
OPT_TAG=true
OPT_TAG_GITHUB=true
OPT_TAG_GITHUBSTARS=true
OPT_TAG_LANGUAGE=false
OPT_TAG_USERNAME=false
```

## Usage
Expand All @@ -43,6 +48,25 @@ To directly update an existing collection without an interactive menu, run:
```bash
python starwarden.py -id YOUR_COLLECTION_ID
```
## Environment Variables

| Name | Default | Description
| :--- | :---: | :---
| GITHUB_TOKEN || GitHub API token
| GITHUB_USERNAME|| GitHub username
| LINKWARDEN_URL || Linkwarden URL https://your-linkwarden-instance.com
| LINKWARDEN_TOKEN| | Linkwarden API token
| COLLECTION_ID || Linkwarden Collection ID to update (Number) /collections/499
| CRON_SCHEDULE | 0 6 * * * | Cron Schedule (default is daily at 6am)
||(True/False)|
| OPT_TAG | true| Enable/Disable all Tagging
| OPT_TAG_GITHUB | true | Tag Link with "GitHub"
| OPT_TAG_GITHUBSTARS | true| Tag Link with "GitHub Stars"
| OPT_TAG_LANGUAGE | false| Tag Link with Language of repo (e.g. Python or JavaScript)
| OPT_TAG_USERNAME | false| Tag GitHub username
||-|
| OPT_TAG_CUSTOM | | Add custom tags, separated by commas (e.g. tag1,tag2)


## Unsupervised Updates

Expand All @@ -59,6 +83,11 @@ For automated, unsupervised updates, you can use Docker with the provided docker
LINKWARDEN_TOKEN=your_linkwarden_api_token
COLLECTION_ID=your_linkwarden_collection_id
CRON_SCHEDULE=0 0 * * * # Run daily at midnight, adjust as needed
OPT_TAG=true
OPT_TAG_GITHUB=true
OPT_TAG_GITHUBSTARS=true
OPT_TAG_LANGUAGE=false
OPT_TAG_USERNAME=false
```

3. Use the following `docker-compose.yml` file:
Expand Down
33 changes: 28 additions & 5 deletions starwarden.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ def get_existing_links(self, collection_id):

seen_links.update(new_links)
yield from new_links
cursor += 50
cursor = links[len(links)-1].get("id")

except requests.RequestException as e:
logger.error(f"Error fetching links from cursor {cursor}: {str(e)}")
if hasattr(e, "response") and e.response is not None:
Expand Down Expand Up @@ -186,7 +187,7 @@ def create_collection(self, name, description=""):
logger.error(f"Response content: {e.response.text}")
return None

def upload_link(self, collection_id, repo):
def upload_link(self, collection_id, repo, tags):
description = repo.description or ""
if len(description) > 2048:
# Truncate and add ellipsis so final length is 2048
Expand All @@ -196,9 +197,9 @@ def upload_link(self, collection_id, repo):
"title": repo.full_name,
"description": description,
"collection": {"id": collection_id},
"tags": [{"name": "GitHub"}, {"name": "GitHub Stars"}],
}

if tags:
link_data["tags"] = tags
logger.debug(
f"Sending link data to Linkwarden: {json.dumps(link_data, indent=2)}"
)
Expand Down Expand Up @@ -267,6 +268,12 @@ def load_env(self):
self.github_username = os.getenv("GITHUB_USERNAME")
self.linkwarden_url = os.getenv("LINKWARDEN_URL")
self.linkwarden_token = os.getenv("LINKWARDEN_TOKEN")
self.opt_tag = os.getenv('OPT_TAG', 'false').lower() == 'true'
self.opt_tag_github = os.getenv('OPT_TAG_GITHUB', 'false').lower() == 'true'
self.opt_tag_githubStars = os.getenv('OPT_TAG_GITHUBSTARS', 'false').lower() == 'true'
self.opt_tag_language = os.getenv('OPT_TAG_LANGUAGE', 'false').lower() == 'true'
self.opt_tag_username = os.getenv('OPT_TAG_USERNAME', 'false').lower() == 'true'
self.opt_tag_custom = os.getenv('OPT_TAG_CUSTOM') if os.getenv('OPT_TAG_CUSTOM','false').lower() != 'false' and len(os.getenv('OPT_TAG_CUSTOM'))>0 else False

if not all([self.github_username, self.linkwarden_url, self.linkwarden_token]):
logger.error(
Expand Down Expand Up @@ -456,11 +463,27 @@ def run(self):

max_retries = 3
retry_count = 0
tags = []
if self.opt_tag:
if self.opt_tag_github:
tags.append({"name": "GitHub"})
if self.opt_tag_githubStars:
tags.append({"name": "GitHub Stars"})
if self.opt_tag_language and repo.language:
tags.append({"name": repo.language})
if self.opt_tag_username:
tags.append({"name": self.github_username})
if self.opt_tag_custom:
for tag in self.opt_tag_custom.split(','):
if len(tag)>0:
tags.append({"name": tag.strip()})


while retry_count < max_retries:
try:
logger.info(f"Processing repository: {repo.full_name}")
link_id = self.linkwarden_manager.upload_link(
collection_id, repo
collection_id, repo, tags
)

if link_id:
Expand Down
Loading