2019-03-23 03:32:38 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding:utf-8 -*-
|
|
|
|
|
|
|
|
import requests
|
|
|
|
import utils
|
2019-03-24 18:26:53 +03:00
|
|
|
import packages.trend.github_lang as github_lang
|
|
|
|
from re import search, escape
|
2019-03-23 09:01:03 +03:00
|
|
|
from bs4 import BeautifulSoup
|
2019-03-23 03:32:38 +03:00
|
|
|
|
|
|
|
def github(string, entities):
|
2019-03-23 09:01:03 +03:00
|
|
|
"""Grab the GitHub trends"""
|
2019-03-24 18:26:53 +03:00
|
|
|
|
2019-03-24 16:16:38 +03:00
|
|
|
# Number of repositories
|
|
|
|
limit = 5
|
2019-03-24 18:26:53 +03:00
|
|
|
|
2019-03-24 16:16:38 +03:00
|
|
|
# Range string
|
|
|
|
since = 'daily'
|
|
|
|
|
2019-03-24 18:26:53 +03:00
|
|
|
# Language slug
|
|
|
|
langslug = ''
|
|
|
|
|
2019-03-24 16:16:38 +03:00
|
|
|
for item in entities:
|
|
|
|
if item['entity'] == 'number':
|
|
|
|
limit = item['resolution']['value']
|
|
|
|
if item['entity'] == 'daterange':
|
|
|
|
if item['resolution']['timex'].find('W') != -1:
|
|
|
|
since = 'weekly'
|
|
|
|
else:
|
|
|
|
since = 'monthly'
|
|
|
|
|
2019-03-24 18:26:53 +03:00
|
|
|
# Feed the languages list based on the GitHub languages list
|
|
|
|
for i, language in enumerate(github_lang.getall()):
|
|
|
|
# Find the asked language
|
|
|
|
if search(r'\b' + escape(language.lower()) + r'\b', string.lower()):
|
|
|
|
langslug = language.lower()
|
|
|
|
|
2019-03-24 16:16:38 +03:00
|
|
|
if limit > 25:
|
|
|
|
utils.output('inter', 'limit_max', utils.translate('limit_max'))
|
|
|
|
limit = 25
|
|
|
|
elif limit == 0:
|
|
|
|
limit = 5
|
|
|
|
|
2019-03-23 09:01:03 +03:00
|
|
|
utils.output('inter', 'reaching', utils.translate('reaching'))
|
|
|
|
|
|
|
|
try:
|
2019-03-24 18:26:53 +03:00
|
|
|
r = utils.http('GET', 'https://github.com/trending/' + langslug + '?since=' + since)
|
2019-03-23 09:01:03 +03:00
|
|
|
soup = BeautifulSoup(r.text, features='html.parser')
|
2019-03-24 10:25:47 +03:00
|
|
|
elements = soup.select('.repo-list li', limit=limit)
|
2019-03-24 12:34:05 +03:00
|
|
|
result = ''
|
2019-03-23 09:01:03 +03:00
|
|
|
|
2019-03-24 10:25:47 +03:00
|
|
|
for i, element in enumerate(elements):
|
|
|
|
repository = element.h3.get_text(strip=True).replace(' ', '')
|
|
|
|
author = element.img.get('alt')[1:]
|
2019-03-24 18:26:53 +03:00
|
|
|
stars = element.select('span.d-inline-block.float-sm-right')[0].get_text(strip=True).split(' ')[0]
|
|
|
|
separators = [' ', ',', '.']
|
|
|
|
|
|
|
|
# Replace potential separators number
|
|
|
|
for j, separator in enumerate(separators):
|
|
|
|
stars = stars.replace(separator, '')
|
2019-03-24 10:25:47 +03:00
|
|
|
|
2019-03-24 12:34:05 +03:00
|
|
|
result += utils.translate('list_element', {
|
|
|
|
'rank': i + 1,
|
|
|
|
'repository_url': 'https://github.com/' + repository,
|
|
|
|
'repository_name': repository,
|
|
|
|
'author_url': 'https://github.com/' + author,
|
2019-03-24 18:26:53 +03:00
|
|
|
'author_username': author,
|
|
|
|
'stars_nb': stars
|
2019-03-24 10:25:47 +03:00
|
|
|
}
|
|
|
|
)
|
2019-03-24 12:34:05 +03:00
|
|
|
|
|
|
|
utils.output('end', 'done', utils.translate('today', {
|
|
|
|
'limit': limit,
|
|
|
|
'result': result
|
|
|
|
}
|
2019-03-24 10:25:47 +03:00
|
|
|
)
|
2019-03-24 12:34:05 +03:00
|
|
|
)
|
2019-03-23 09:01:03 +03:00
|
|
|
except requests.exceptions.RequestException as e:
|
|
|
|
return utils.output('end', 'unreachable', utils.translate('unreachable'))
|