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
|
|
|
|
2019-05-03 18:31:09 +03:00
|
|
|
def run(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-28 15:33:05 +03:00
|
|
|
# Technology slug
|
|
|
|
techslug = ''
|
|
|
|
|
|
|
|
# Technology name
|
|
|
|
tech = ''
|
|
|
|
|
|
|
|
# Answer key
|
|
|
|
answerkey = 'today'
|
2019-03-24 18:26:53 +03:00
|
|
|
|
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'
|
2019-03-28 15:33:05 +03:00
|
|
|
answerkey = 'week'
|
2019-03-24 16:16:38 +03:00
|
|
|
else:
|
|
|
|
since = 'monthly'
|
2019-03-28 15:33:05 +03:00
|
|
|
answerkey = 'month'
|
2019-03-24 16:16:38 +03:00
|
|
|
|
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()):
|
2019-03-28 15:33:05 +03:00
|
|
|
answerkey += '_with_tech'
|
|
|
|
tech = language
|
|
|
|
techslug = language.lower()
|
2019-03-24 18:26:53 +03:00
|
|
|
|
2019-03-24 16:16:38 +03:00
|
|
|
if limit > 25:
|
2019-03-28 15:33:05 +03:00
|
|
|
utils.output('inter', 'limit_max', utils.translate('limit_max', {
|
|
|
|
'limit': limit
|
|
|
|
}))
|
2019-03-24 16:16:38 +03:00
|
|
|
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-28 15:33:05 +03:00
|
|
|
r = utils.http('GET', 'https://github.com/trending/' + techslug + '?since=' + since)
|
2019-03-23 09:01:03 +03:00
|
|
|
soup = BeautifulSoup(r.text, features='html.parser')
|
2019-06-17 19:08:20 +03:00
|
|
|
elements = soup.select('article.Box-row', 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):
|
2019-06-17 19:08:20 +03:00
|
|
|
repository = element.h1.get_text(strip=True).replace(' ', '')
|
2019-04-04 13:11:54 +03:00
|
|
|
if (element.img != None):
|
|
|
|
author = element.img.get('alt')[1:]
|
|
|
|
else:
|
|
|
|
author = '?'
|
2019-03-24 18:26:53 +03:00
|
|
|
|
2019-04-25 03:10:01 +03:00
|
|
|
hasstars = element.select('span.d-inline-block.float-sm-right')
|
|
|
|
stars = 0
|
|
|
|
|
|
|
|
if hasstars:
|
|
|
|
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
|
|
|
|
2019-03-31 12:28:36 +03:00
|
|
|
return utils.output('end', answerkey, utils.translate(answerkey, {
|
2019-03-24 12:34:05 +03:00
|
|
|
'limit': limit,
|
2019-03-28 15:33:05 +03:00
|
|
|
'tech': tech,
|
2019-03-24 12:34:05 +03:00
|
|
|
'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'))
|