<commit_before># Web Database Classification - hw2
# Advanced Database Systems
# <NAME> - pff2108
# <NAME> - rja2139
<commit_msg>Bing query not working<commit_after># Web Database Classification - hw2
# Advanced Database Systems
# <NAME> - pff2108
# <NAME> - rja2139

import urllib2
import base64
import json


def run_query(query):
    # Execute query
    site = "fifa.com"
    query = "premiership"
    query_url = urllib2.quote("'site:" + site + " " + query + "'")
    #bing_url = 'https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/v1/Composite?Query=' + query_url + '&$top=10&$format=json'
    bing_url = 'https://api.datamarket.azure.com/Bing/Search/Web?Query=' + query_url + '&$top=10&$format=json'
    print bing_url
    account_key = '<KEY>'
    # account_key = bing

    account_key_enc = base64.b64encode(account_key + ':' + account_key)
    headers = {'Authorization': 'Basic ' + account_key_enc}
    req = urllib2.Request(bing_url, headers=headers)
    response = urllib2.urlopen(req)
    content = response.read()
    # content contains the xml/json response from Bing.
    tree = json.loads(content)
    # return tree['d']['results']
    return tree

#https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/v1/Composite?Query=%27site%3Afifa.com%20premiership%27&$top=10&$format=json
#https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/v1/Composite?Query=%27site%3afifa.com%20premiership%27&$top=10&$format=Atom

result = run_query('oi')
print result
{'fraction_non_alphanumeric': 0.1485482781904119, 'fraction_numerical': 0.03578663065496286, 'mean_word_length': 4.408759124087592, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1003698', 'n_tokens_mistral': 573, 'n_tokens_neox': 535, 'n_words': 92}