<commit_before>import { Injectable } from '@angular/core';
import { Http, Headers, ResponseContentType } from '@angular/http';
import { Observable } from 'rxjs';

import { ErrorHandler } from './http-errors'
import { AppUtilService } from './app-util.service';

@Injectable()
export class AlveoService {
  selectedList: any;
  lists: Array<any>;

  constructor(
    public http: Http,
    public appService: AppUtilService)
  {
    this.appService.database.get('lists').then(result => this.lists = result.lists);
    //this.startStore();
  }

  apiRequest(url, successCallback): void {
    let header = this.appService.auth.buildHeader();
    header.append('X-Api-Key', this.appService.auth.apiKey);

    this.http.get(url, this.appService.auth.buildOptions(header))
                .subscribe(data => successCallback(data),
                           error => ErrorHandler(error, this));
  }

  apiFileRequest(url, successCallback): void {
    let header = new Headers({
        'Accept': 'application/json',
        'responseType': String(ResponseContentType.ArrayBuffer)
      });
    header.append('X-Api-Key', this.appService.auth.apiKey);

    this.http.get(url, this.appService.auth.buildOptions(header))
                .subscribe(data => successCallback(data),
                           error => ErrorHandler(error, this));
  }

  getActiveList(): any {
    return this.selectedList;
  }

  getActiveListData(): any {
    // If list doesn't contain data, pull it
    if (this.selectedList['_tt_preload'] == undefined) {
      // Guard against multiple calls?
      console.log("Looks like I don't have that list preloaded, retrieving it now.");
      this.pullList(this.selectedList);

      return [];
    }
    return this.selectedList['_tt_preload'];
  }

  getListItemData(list: any): any {
    // If list doesn't contain data, pull it
    if (list.url == undefined) {
      // Guard against multiple calls?
      console.log("Looks like I don't have that list preloaded, call getListData() first.");
      return {data:{}};
    }

    if (list.data == undefined) {
      console.log("Looks like I don't have that lists' data preloaded, retrieving it now.");
      this.pullItem(list)
      return {data:{}};
    }
    return list;
  }

  pullIndex(): void { 
    console.log("Pulled");
    this.apiFileRequest("https://staging.alveo.edu.au/catalog/austalk/1_114_3_8_001/document/1_114_3_8_001-ch6-speaker.wav",
      (data) => {
        console.log(data);
      });
    // Pulls an array of all the lists from Alveo
    this.apiRequest(this.appService.auth.baseURL + '/item_lists',
      (data) => {
        let lists = [];
        lists = lists.concat(data.json().own);
        lists = lists.concat(data.json().shared);

        this.lists = lists;
      });
  }

  pullLists(lists: Array<any>): void {
    for (let list of lists) {
      this.pullList(list);
    }
  }

  pullList(list: any, chainload=false): void {
    this.apiRequest(list.item_list_url, (data) => {
      list['_tt_preload'] = [];
      for (let item_url of data.json().items) {<commit_msg>Implemented basic (and functional) audio downloading<commit_after>import { Injectable } from '@angular/core';
import { Http, Headers, ResponseContentType } from '@angular/http';
import { Observable } from 'rxjs';

import { ErrorHandler } from './http-errors'
import { AppUtilService } from './app-util.service';

import { Audio } from './audio';

@Injectable()
export class AlveoService {
  selectedList: any;
  lists: Array<any>;
  audioData: Audio;

  constructor(
    public http: Http,
    public appService: AppUtilService)
  {
    this.appService.database.get('lists').then(result => this.lists = result.lists);
    //this.startStore();
  }

  apiRequest(url, successCallback): void {
    let header = this.appService.auth.buildHeader();
    header.append('X-Api-Key', this.appService.auth.apiKey);

    this.http.get(url, this.appService.auth.buildOptions(header))
                .subscribe(data => successCallback(data),
                           error => ErrorHandler(error, this));
  }

  apiFileRequest(url, successCallback): void {
    let header = this.appService.auth.buildHeader();
    header.append('X-Api-Key', this.appService.auth.apiKey);

    let options = this.appService.auth.buildOptions(header);
    options['responsetype'] = String(ResponseContentType.ArrayBuffer);

    this.http.get(url, this.appService.auth.buildOptions(header))
                .subscribe(data => successCallback(data),
                           error => ErrorHandler(error, this));
  }

  getActiveList(): any {
    return this.selectedList;
  }

  getActiveListData(): any {
    // If list doesn't contain data, pull it
    if (this.selectedList['_tt_preload'] == undefined) {
      // Guard against multiple calls?
      console.log("Looks like I don't have that list preloaded, retrieving it now.");
      this.pullList(this.selectedList);

      return [];
    }
    return this.selectedList['_tt_preload'];
  }

  getListItemData(list: any): any {
    // If list doesn't contain data, pull it
    if (list.url == undefined) {
      // Guard against multiple calls?
      console.log("Looks like I don't have that list preloaded, call getListData() first.");
      return {data:{}};
    }

    if (list.data == undefined) {
      console.log("Looks like I don't have that lists' data preloaded, retrieving it now.");
      this.pullItem(list)
      return {data:{}};
    }
    return list;
  }

  pullIndex(): void { 
    // Pulls an array of all the lists from Alveo
    this.apiRequest(this.appService.auth.baseURL + '/item_lists',
      (data) => {
        let lists = [];
        lists = lists.concat(data.json().own);
        lists = lists.concat(data.json().shared);

        this.lists = lists;
      });
  }

  pullLists(lists: Array<any>): void {
    for (let list of lists) {
      this.pullList(list);
    }
  }

  pullList(list: any, chainload=false): void {
    this.apiRequest(list.item_list_url, (data) => {
      list['_tt_preload'] = [];
      for (let item_url of data.json().items) {
{'fraction_non_alphanumeric': 0.1410488245931284, 'fraction_numerical': 0.0031234588196613513, 'mean_word_length': 2.892514395393474, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 22, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '482405', 'n_tokens_mistral': 1893, 'n_tokens_neox': 1827, 'n_words': 423}