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

Introduced search support for multiple programming languages #116

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 12 additions & 4 deletions starcli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import json
import os
from datetime import datetime, timedelta
from pathlib import Path

from xdg import xdg_cache_home
from xdg import BaseDirectory

from .layouts import print_results, shorten_count
from .search import (
Expand All @@ -19,12 +20,19 @@


# could be made into config option in the future
CACHED_RESULT_PATH = xdg_cache_home() / "starcli.json"
CACHED_RESULT_PATH = Path(BaseDirectory.xdg_cache_home) / "starcli.json"
Copy link
Contributor Author

@dzmitry-kankalovich dzmitry-kankalovich Dec 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is weird, but basically doing pip3 install -r requirements_dev.txt installs a version of xdg that does not export xdg_cache_home as is. Possibly a breaking change in some of the minor/patch version of xdg? I've double-checked it with fresh venv - same problem.

Copy link
Owner

@hedyhli hedyhli Jan 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's weird, I've tested with python 3.8 and 3.10, on both ubuntu and arch. Are you having this issue by installing from PyPI as well?

What is your OS and python version?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, it seems like you might be using pyxdg, as xdg doesn't have BaseDirectory.

CACHE_EXPIRATION = 1 # Minutes


@click.command()
@click.option("--lang", "-l", type=str, default="", help="Language filter eg: python")
@click.option(
"--lang",
"-l",
multiple=True,
type=str,
default=[""],
help="Language filter eg: python",
)
@click.option(
"--spoken-language",
"-S",
Expand Down Expand Up @@ -111,7 +119,7 @@
)
@click.option("--debug", is_flag=True, default=False, help="Turn on debugging mode")
def cli(
lang,
lang: list[str],
dzmitry-kankalovich marked this conversation as resolved.
Show resolved Hide resolved
spoken_language,
created,
topic,
Expand Down
20 changes: 11 additions & 9 deletions starcli/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def search_error(status_code):


def search(
language=None,
languages=[""],
created=None,
pushed=None,
stars=">=100",
Expand Down Expand Up @@ -207,7 +207,7 @@ def search(

query += f"stars:{stars}+created:{created_str}" # construct query
query += f"+pushed:{pushed_str}" # add pushed info to query
query += f"+language:{language}" if language else "" # add language to query
query += f"".join([f"+language:{language}" for language in languages]) # add language to query
query += f"".join(["+topic:" + i for i in topics]) # add topics to query

url = f"{API_URL}?q={query}&sort=stars&order={order}" # use query to construct url
Expand All @@ -226,15 +226,17 @@ def search(


def search_github_trending(
language=None, spoken_language=None, order="desc", stars=">=10", date_range=None
languages=[""], spoken_language=None, order="desc", stars=">=10", date_range=None
):
"""Returns trending repositories from github trending page"""
if date_range:
gtrending_repo_list = fetch_repos(
language, spoken_language, date_range_map[date_range]
)
else:
gtrending_repo_list = fetch_repos(language, spoken_language)
gtrending_repo_list = []
for language in languages:
dzmitry-kankalovich marked this conversation as resolved.
Show resolved Hide resolved
if date_range:
gtrending_repo_list += fetch_repos(
language, spoken_language, date_range_map[date_range]
)
else:
gtrending_repo_list += fetch_repos(language, spoken_language)
repositories = []
for gtrending_repo in gtrending_repo_list:
repo_dict = convert_repo_dict(gtrending_repo)
Expand Down
32 changes: 23 additions & 9 deletions tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
def test_search_language():
"""Test searching by language"""
for language in ["python", "Python", "JavaScript", "c"]:
repos = search(language)
repos = search([language])
for repo in repos:
assert repo["stargazers_count"] >= 0
assert repo["watchers_count"] >= 0
Expand All @@ -31,7 +31,7 @@ def test_search_topics():
"algorithm",
"shell",
]:
repos = search(language="python", topics=topics)
repos = search(languages=["python"], topics=topics)
for repo in repos:
assert repo["stargazers_count"] >= 0
assert repo["watchers_count"] >= 0
Expand All @@ -50,7 +50,7 @@ def test_search_created_date():
created_date_value = (datetime.utcnow() + timedelta(days=day_range)).strftime(
date_format
)
repos = search(language="python", created=created_date_value)
repos = search(languages=["python"], created=created_date_value)
for repo in repos:
assert repo["stargazers_count"] >= 0
assert repo["watchers_count"] >= 0
Expand All @@ -77,7 +77,7 @@ def test_search_pushed_date():
pushed_date_value = (datetime.utcnow() + timedelta(days=day_range)).strftime(
date_format
)
repos = search(language="python", pushed=pushed_date_value)
repos = search(languages=["python"], pushed=pushed_date_value)
for repo in repos:
assert repo["stargazers_count"] >= 0
assert repo["watchers_count"] >= 0
Expand All @@ -100,7 +100,7 @@ def test_search_pushed_date():

def test_search_stars():
"""Test searching with number of stars"""
repos = search(language="python", stars="<10")
repos = search(languages=["python"], stars="<10")
for repo in repos:
# FIXME: Possibly problem with GitHub API?
assert repo["stargazers_count"] < 10 # Somestimes stars+1
Expand All @@ -113,7 +113,7 @@ def test_search_stars():

def test_search_user():
"""Test searching by user"""
repos = search(language="ruby", user="octocat")
repos = search(languages=["ruby"], user="octocat")
for repo in repos:
assert repo["stargazers_count"] >= 0
assert repo["watchers_count"] >= 0
Expand All @@ -125,13 +125,13 @@ def test_search_user():

def test_no_results():
"""Test if no search results found"""
repos = search("python", "2020-01-01", "2019-01-01")
repos = search(["python"], "2020-01-01", "2019-01-01")
assert repos == []


def test_spoken_language():
"""Test search by spoken languages"""
repos = search_github_trending("javascript", "zh") # zh = chinese
repos = search_github_trending(["javascript"], "zh") # zh = chinese
for repo in repos:
assert repo["stargazers_count"] >= 0
assert repo["language"].lower() == "javascript"
Expand All @@ -143,11 +143,25 @@ def test_spoken_language():
def test_date_range():
"""Test search by date range"""
for date_range in ["daily", "monthly", "weekly"]:
repos = search_github_trending("python", "en", date_range)
repos = search_github_trending(["python"], "en", date_range)
for repo in repos:
assert repo["stargazers_count"] >= 0
assert repo["language"].lower() == "python"
assert (repo["description"] == None) or repo["description"]
assert repo["full_name"].count("/") >= 1
assert repo["html_url"] == "https://github.com/" + repo["full_name"]
# TODO: verify date range


def test_search_multiple_language():
"""Test searching by multiple language"""
languages = ["python", "c"]
repos = search(languages)
for repo in repos:
assert repo["stargazers_count"] >= 0
assert repo["watchers_count"] >= 0
assert repo["language"].lower() in languages
assert (repo["description"] is None) or repo["description"]
assert repo["full_name"].count("/") == 1
assert repo["full_name"] == f"{repo['owner']['login']}/{repo['name']}"
assert repo["html_url"] == "https://github.com/" + repo["full_name"]