#from phabricator import Phabricator
import os, sys
import json
import numpy as np
import pandas as pd
import requests
import re
import datetime
import time
from urllib.parse import quote_plus

from requests.auth import HTTPDigestAuth
from pygerrit2 import GerritRestAPI, HTTPBasicAuth

# GET /changes/?q=status:abandoned&q=before:{date}&q={TERM}
# GET https://gerrit.wikimedia.org/r/changes/?q=status:abandoned+visualeditor

def query_changes(
    query_terms, 
    limit = 100, 
    api_url_base = 'https://gerrit.wikimedia.org/',
    sleep = 10
):
    
    time.sleep(sleep)
    to_query = 1
    after = None

    data = []

    while to_query == 1:
        time.sleep(sleep)
        joined_terms = "+".join(query_terms)
        params = {
            'q'  : joined_terms,
        }
        
        api_url = f"{api_url_base}r/changes/?q={joined_terms}"
        #add no-limit and API key somewhere

        #auth = HTTPBasicAuth("ggonnemm", "1V6txZh5X+N3JpDMm5zqZM2M7ewA5D09g4ABOZAl5Q")
        response = requests.get(api_url, headers={'Content-Type': 'application/json'})
        
        result = json.load(response.text[5:])
        #print(response.text)
        ## the data
        data_tmp = result
        data += data_tmp
        print(data[-1])
        ## check if there are more results to query
        break
    return data


if __name__ == "__main__":
    query_strings = ['before:2016-12-31', 'visualeditor']
    results = query_changes(query_strings)
    print(results)