-
Notifications
You must be signed in to change notification settings - Fork 13
/
github_worker.py
83 lines (60 loc) · 2.16 KB
/
github_worker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
import requests
import redis
import pika
from ulduz.constants import GITHUB_API_ENDPOINT, GITHUB_STAR_KEY, \
GITHUB_REPO_KEY, GITHUB_WORKER_QUEUE
RABBIT_HOST = os.getenv('RABBIT_HOST')
RABBIT_PORT = os.getenv('RABBIT_PORT', 5672)
VIRTUAL_HOST = os.getenv('VIRTUAL_HOST')
RABBIT_USER = os.getenv('RABBIT_USER')
RABBIT_PASSWORD = os.getenv('RABBIT_PASSWORD')
REDIS_HOST = os.getenv('REDIS_HOST')
REDIS_PORT = os.getenv('REDIS_PORT', 6379)
try:
github_token = os.getenv("GITHUB_TOKEN")
except KeyError:
raise Exception("Please set environment variables GITHUB_TOKEN")
github_api_url = GITHUB_API_ENDPOINT
github_params = {'access_token': github_token}
r = redis.Redis(host=REDIS_HOST, port=REDIS_PORT)
def is_exists_in_db(sub, repo_id):
return r.sismember(GITHUB_STAR_KEY.format(sub), repo_id)
def add_into_db(sub, starred_repo):
_id = starred_repo.get('id')
r.sadd(GITHUB_STAR_KEY.format(sub), _id) #add repo_id's to subscription set
r.hmset(
GITHUB_REPO_KEY.format(_id),
{
'name': starred_repo['name'],
'url': starred_repo['html_url'],
}
) # add repo_name and repo_url to repo_id's hash
def on_request(ch, method, properties, github_sub):
_url = github_api_url.format(github_sub)
github_params["page"] = 1
while True:
response = requests.get(url=_url, params=github_params).json()
if not response:
break
for repo in response:
if is_exists_in_db(github_sub, repo):
break
else:
add_into_db(github_sub, repo)
github_params["page"] += 1
connection = pika.BlockingConnection(
pika.ConnectionParameters(
host=RABBIT_HOST,
virtual_host=VIRTUAL_HOST,
credentials=pika.PlainCredentials(RABBIT_USER, RABBIT_PASSWORD)
)
)
channel = connection.channel()
channel.exchange_declare(exchange='work',
exchange_type='direct')
channel.queue_bind(exchange='work',
queue='github_queue',
routing_key='github')
channel.basic_consume(on_request, queue=GITHUB_WORKER_QUEUE, no_ack=True)
channel.start_consuming()