Skip to content

Commit 906d80c

Browse files
authored
Merge pull request #22 from PawsFunctions/main
2 parents d5fb060 + bcba874 commit 906d80c

File tree

4 files changed

+72
-5
lines changed

4 files changed

+72
-5
lines changed

.env.example

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,11 @@ LINKWARDEN_TOKEN=your_linkwarden_api_token_here
88
#### FOR CRONTAB: ####
99
COLLECTION_ID=30
1010
CRON_SCHEDULE="0 6 * * *" # Cron Schedule (default is daily at 6am)
11+
12+
#### Tagging Options: ####
13+
OPT_TAG=true # Enable/Disable all Tagging
14+
OPT_TAG_GITHUB=true # Tag Link with "GitHub"
15+
OPT_TAG_GITHUBSTARS=true # Tag Link with "GitHub Stars"
16+
OPT_TAG_LANGUAGE=false # Tag Link with Language of repo (e.g. Python or JavaScript)
17+
OPT_TAG_USERNAME=false # Tag GitHub username
18+
OPT_TAG_CUSTOM=false # Add custom tags, separated by commas (e.g. tag1,tag2)

Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ ENV LINKWARDEN_TOKEN=""
88
ENV COLLECTION_ID=""
99
ENV CRON_SCHEDULE="0 6 * * *"
1010

11+
ENV OPT_TAG=true
12+
ENV OPT_TAG_GITHUB=true
13+
ENV OPT_TAG_GITHUBSTARS=true
14+
ENV OPT_TAG_LANGUAGE=false
15+
ENV OPT_TAG_USERNAME=false
16+
ENV OPT_TAG_CUSTOM=false
17+
1118
WORKDIR /app
1219

1320
RUN apt-get update && apt-get install -y cron && rm -rf /var/lib/apt/lists/*

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ StarWarden allows you to export GitHub stars to Linkwarden.
2828
GITHUB_USERNAME=your_github_username
2929
LINKWARDEN_URL=your_linkwarden_instance_url
3030
LINKWARDEN_TOKEN=your_linkwarden_api_token
31+
OPT_TAG=true
32+
OPT_TAG_GITHUB=true
33+
OPT_TAG_GITHUBSTARS=true
34+
OPT_TAG_LANGUAGE=false
35+
OPT_TAG_USERNAME=false
3136
```
3237

3338
## Usage
@@ -43,6 +48,25 @@ To directly update an existing collection without an interactive menu, run:
4348
```bash
4449
python starwarden.py -id YOUR_COLLECTION_ID
4550
```
51+
## Environment Variables
52+
53+
| Name | Default | Description
54+
| :--- | :---: | :---
55+
| GITHUB_TOKEN || GitHub API token
56+
| GITHUB_USERNAME|| GitHub username
57+
| LINKWARDEN_URL || Linkwarden URL https://your-linkwarden-instance.com
58+
| LINKWARDEN_TOKEN| | Linkwarden API token
59+
| COLLECTION_ID || Linkwarden Collection ID to update (Number) /collections/499
60+
| CRON_SCHEDULE | 0 6 * * * | Cron Schedule (default is daily at 6am)
61+
||(True/False)|
62+
| OPT_TAG | true| Enable/Disable all Tagging
63+
| OPT_TAG_GITHUB | true | Tag Link with "GitHub"
64+
| OPT_TAG_GITHUBSTARS | true| Tag Link with "GitHub Stars"
65+
| OPT_TAG_LANGUAGE | false| Tag Link with Language of repo (e.g. Python or JavaScript)
66+
| OPT_TAG_USERNAME | false| Tag GitHub username
67+
||-|
68+
| OPT_TAG_CUSTOM | | Add custom tags, separated by commas (e.g. tag1,tag2)
69+
4670

4771
## Unsupervised Updates
4872

@@ -59,6 +83,11 @@ For automated, unsupervised updates, you can use Docker with the provided docker
5983
LINKWARDEN_TOKEN=your_linkwarden_api_token
6084
COLLECTION_ID=your_linkwarden_collection_id
6185
CRON_SCHEDULE=0 0 * * * # Run daily at midnight, adjust as needed
86+
OPT_TAG=true
87+
OPT_TAG_GITHUB=true
88+
OPT_TAG_GITHUBSTARS=true
89+
OPT_TAG_LANGUAGE=false
90+
OPT_TAG_USERNAME=false
6291
```
6392

6493
3. Use the following `docker-compose.yml` file:

starwarden.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ def get_existing_links(self, collection_id):
134134

135135
seen_links.update(new_links)
136136
yield from new_links
137-
cursor += 50
137+
cursor = links[len(links)-1].get("id")
138+
138139
except requests.RequestException as e:
139140
logger.error(f"Error fetching links from cursor {cursor}: {str(e)}")
140141
if hasattr(e, "response") and e.response is not None:
@@ -186,7 +187,7 @@ def create_collection(self, name, description=""):
186187
logger.error(f"Response content: {e.response.text}")
187188
return None
188189

189-
def upload_link(self, collection_id, repo):
190+
def upload_link(self, collection_id, repo, tags):
190191
description = repo.description or ""
191192
if len(description) > 2048:
192193
# Truncate and add ellipsis so final length is 2048
@@ -196,9 +197,9 @@ def upload_link(self, collection_id, repo):
196197
"title": repo.full_name,
197198
"description": description,
198199
"collection": {"id": collection_id},
199-
"tags": [{"name": "GitHub"}, {"name": "GitHub Stars"}],
200200
}
201-
201+
if tags:
202+
link_data["tags"] = tags
202203
logger.debug(
203204
f"Sending link data to Linkwarden: {json.dumps(link_data, indent=2)}"
204205
)
@@ -267,6 +268,12 @@ def load_env(self):
267268
self.github_username = os.getenv("GITHUB_USERNAME")
268269
self.linkwarden_url = os.getenv("LINKWARDEN_URL")
269270
self.linkwarden_token = os.getenv("LINKWARDEN_TOKEN")
271+
self.opt_tag = os.getenv('OPT_TAG', 'false').lower() == 'true'
272+
self.opt_tag_github = os.getenv('OPT_TAG_GITHUB', 'false').lower() == 'true'
273+
self.opt_tag_githubStars = os.getenv('OPT_TAG_GITHUBSTARS', 'false').lower() == 'true'
274+
self.opt_tag_language = os.getenv('OPT_TAG_LANGUAGE', 'false').lower() == 'true'
275+
self.opt_tag_username = os.getenv('OPT_TAG_USERNAME', 'false').lower() == 'true'
276+
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
270277

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

457464
max_retries = 3
458465
retry_count = 0
466+
tags = []
467+
if self.opt_tag:
468+
if self.opt_tag_github:
469+
tags.append({"name": "GitHub"})
470+
if self.opt_tag_githubStars:
471+
tags.append({"name": "GitHub Stars"})
472+
if self.opt_tag_language and repo.language:
473+
tags.append({"name": repo.language})
474+
if self.opt_tag_username:
475+
tags.append({"name": self.github_username})
476+
if self.opt_tag_custom:
477+
for tag in self.opt_tag_custom.split(','):
478+
if len(tag)>0:
479+
tags.append({"name": tag.strip()})
480+
481+
459482
while retry_count < max_retries:
460483
try:
461484
logger.info(f"Processing repository: {repo.full_name}")
462485
link_id = self.linkwarden_manager.upload_link(
463-
collection_id, repo
486+
collection_id, repo, tags
464487
)
465488

466489
if link_id:

0 commit comments

Comments
 (0)