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 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
13 changes: 10 additions & 3 deletions starcli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os
from datetime import datetime, timedelta

from xdg import xdg_cache_home
from xdg import XDG_CACHE_HOME

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


# could be made into config option in the future
CACHED_RESULT_PATH = xdg_cache_home() / "starcli.json"
CACHED_RESULT_PATH = XDG_CACHE_HOME / "starcli.json"
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
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 @@ -11,7 +11,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 @@ -33,7 +33,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 @@ -52,7 +52,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 @@ -79,7 +79,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 @@ -103,7 +103,7 @@ def test_search_pushed_date():
@pytest.mark.xfail(raises=AssertionError)
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 @@ -116,7 +116,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 @@ -128,13 +128,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 @@ -146,11 +146,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"]