import moment from 'moment';

import Request from '../../request';
import Item from '../../content/item';

import TraktWebAPIUtils from './trakt-web-api-utils';
import ActivityActionCreators from '../actions/activity-action-creators';

const AUTH_REGEXP = new RegExp("\"authURL\":\"(.*?)\"");
const BUILD_IDENTIFIER_REGEXP = new RegExp("\"BUILD_IDENTIFIER\":\"(.*?)\"");
const NETFLIX_HOST = 'https://www.netflix.com';
const NETFLIX_API_HOST = `${NETFLIX_HOST}/api/shakti`;

export default class NetflixWebAPIUtils {
  static extractAuthURL(response) {
    return AUTH_REGEXP.exec(response)[1];
  }

  static extractBuildIndentifier(response) {
    return BUILD_IDENTIFIER_REGEXP.exec(response)[1];
  }

  static activateAPI() {
    return new Promise((resolve, reject) => {
      Request.send({
        method: 'GET',
        url: `${NETFLIX_HOST}/Activate`,
        success: function(response) {
          let url = `${NETFLIX_API_HOST}/${NetflixWebAPIUtils.extractBuildIndentifier(response)}/viewingactivity?authURL=${NetflixWebAPIUtils.extractAuthURL(response)}`;
          resolve(url);
        },
        error: reject
      });
    });
  }

  static listActivities(url, page) {
    Request.send({
      method: 'GET',
      url: `${url}&pg=${page}`,
      success: (response) => {
        let activities = JSON.parse(response).viewedItems;
        let parsedActivities = activities
          .map(NetflixWebAPIUtils.parseActivity)
          .filter((activity) => !!activity);

        Promise.all(parsedActivities)
          .then(ActivityActionCreators.receiveActivities)
          .catch(ActivityActionCreators.receiveActivitiesFailed);
      },
      error: (response, status) => {
        console.log(response, status);
      }
    });
  }

  static getActivities(page = 0) {
    NetflixWebAPIUtils.activateAPI()
      .then(url => NetflixWebAPIUtils.listActivities(url, page));
  }

  static parseActivity(activity) {
    let date = moment(activity.date);
    let item;
    let type = activity.series == undefined ? 'movie' : 'show';

    if (type === 'show') {
      let title = activity.seriesTitle;
      let splittedTitle = activity.title.split(':');
      let season = splittedTitle[0].match(/\d+/g);
      if (season) {
        season = parseInt(season[0]);
      } else {
        return;
      }
      let epTitle = splittedTitle[1].trim().replace('"', '').replace('"', '');

      item = new Item({
        epTitle: epTitle,
        title: title,
        season: season,
        type: type
      });
    } else {
      item = new Item({ title: activity.title, type: type });
    }

    return new Promise((resolve, reject) => {
      let netflix = Object.assign(item, { id: activity.movieID });
      TraktWebAPIUtils.getActivity({ item, date }).then(resolve).catch(reject);
    });
  }
}

@@ -2304,24 +2304,56 @@
 rn;%0A      %7D%0A
+%0A      if(splittedTitle%5B1%5D) %7B%0A  
       let ep
@@ -2422,24 +2422,26 @@
 '');%0A%0A      
+  
 item = new I
@@ -2446,16 +2446,18 @@
  Item(%7B%0A
+  
         
@@ -2482,16 +2482,18 @@
         
+ 
+ 
 title: t
@@ -2494,24 +2494,26 @@
 tle: title,%0A
+  
         seas
@@ -2524,16 +2524,18 @@
 season,%0A
+  
         
@@ -2547,28 +2547,69 @@
  type%0A      
+  
 %7D);%0A
+      %7D else %7B%0A        return;%0A      %7D%0A
     %7D else %7B

Return fix from 86153571a24016a1a8c495738a77d8012fb82a0a that was lost in merges