Skip to content

Commit

Permalink
Fix SmackProvider implementation and Add new source bug
Browse files Browse the repository at this point in the history
  • Loading branch information
allanx2000 committed Jan 18, 2016
1 parent 7984059 commit e4903bd
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 77 deletions.
11 changes: 8 additions & 3 deletions Watcher.Client.WPF/ViewModels/SourceEditorViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,9 @@ public AbstractProvider SelectedProvider
//URLPanel.Visibility = provider.HasUrlField ? Visibility.Visible : Visibility.Collapsed;

//This is to support restoring values from existing, need to rewrite and separate, make clearer?
List<MetaDataObject> meta = originalSource != null ?
List<MetaDataObject> meta = originalSource != null ?
new List<MetaDataObject>(originalSource.Data.GetMetaData().Values) :
value.GetMetaFields();


//TODO: Should create everything here, Grid should just be empty
optionsGrid.Children.Clear();
Expand All @@ -137,8 +136,13 @@ public AbstractProvider SelectedProvider

foreach (var m in meta)
{
//TODO: Temp hack to remove these custom fields, but need to find better, holistic way; redesign framework?
if (m.ID == SourceViewModel.UPDATES_COLOR || m.ID == SourceViewModel.URL)
continue;

RowDefinition rd = new RowDefinition() { Height = GridLength.Auto };
optionsGrid.RowDefinitions.Add(rd);


Label l = new Label();
l.Content = m.DisplayName;
Expand Down Expand Up @@ -246,7 +250,8 @@ public SourceEditorViewModel(Window window, Grid optionsGrid, List<AbstractProvi
//TypeComboBox.ItemsSource = providers;
//TypeComboBox.DisplayMemberPath = "ProviderId";

SetOriginalSource(svm);
if (svm != null)
SetOriginalSource(svm);
}

private void SetOriginalSource(SourceViewModel svm)
Expand Down
2 changes: 1 addition & 1 deletion Watcher.Client.WPF/Watcher.Client.WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<MapFileExtensions>true</MapFileExtensions>
<ProductName>Watcher</ProductName>
<PublisherName>innouvous Technologies</PublisherName>
<ApplicationRevision>20</ApplicationRevision>
<ApplicationRevision>22</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<CreateDesktopShortcut>true</CreateDesktopShortcut>
Expand Down
1 change: 1 addition & 0 deletions Watcher.Provider.SmackProvider/Good Implementation.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

18 changes: 18 additions & 0 deletions Watcher.Provider.SmackProvider/Post.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Watcher.Extensions.V2;

namespace Watcher.Provider.Smack
{
public class Post : AbstractItem
{
public Post(AbstractSource source, string title, string link, int? id = null, bool isNew = true,
DateTime? addedDate = null)
: base(source, title, link, id, isNew, addedDate)
{

}
}
}
169 changes: 96 additions & 73 deletions Watcher.Provider.SmackProvider/SmackProvider.cs
Original file line number Diff line number Diff line change
@@ -1,115 +1,142 @@
using System.Diagnostics;
using System.Runtime.Versioning;
using HtmlAgilityPack;
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Net;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using Watcher.Extensions;
using Watcher.Extensions.V2;

namespace Watcher.Provider.Smack
{
public class Post : AbstractItem
{
public Post(AbstractSource source, string title, string link, int? id = null, bool isNew = true,
DateTime? addedDate = null)
: base(source, title, link, id, isNew, addedDate)
{

}
}

public class SmackProvider : AbstractProvider
{
public const string PROVIDER = "SmackProvider";

public const string META_PAGES = "Pages";

public const string META_SOURCE = "Source";

private const string ChinaSite = "http://www.chinasmack.com/";
private const string JapanSite = "http://www.japancrush.com/";
private int DefaultPages = 3;

private const string ChinaType = "ChinaSMACK";
private const string JapanType = "JapanCRUSH";

public SmackProvider()
: base(PROVIDER)
public SmackProvider() : base(PROVIDER, false, false)
{

}

private const string Site = "Site";
public override List<string> GetMetaFields()
public static class SourceNames
{
return new List<string> { Site };
public const String ChinaSmack = "ChinaSmack";
public const String JapanCrush = "JapanCrush";
}

protected override AbstractSource DoCreateNewSource(string name, string url, Dictionary<string, string> metaData)
//NOTE: Validation depends on selected source type

public static readonly List<string> Sources = new List<string>()
{
if (metaData.ContainsKey(Site))
SourceNames.ChinaSmack,
SourceNames.JapanCrush,
};

public static readonly MetaDataObjectBuilder MetaSource = new MetaDataObjectBuilder(META_SOURCE, "Source", MetaDataObject.Type.Selector, Sources);
public static readonly MetaDataObjectBuilder MetaPages = new MetaDataObjectBuilder(META_PAGES, "Pages");

public override List<MetaDataObject> GetMetaFields()
{
List<MetaDataObject> TEMPLATE = new List<MetaDataObject>()
{
if (metaData[Site].Equals(JapanType, StringComparison.OrdinalIgnoreCase))
metaData[Site] = JapanType;
else if (metaData[Site].Equals(ChinaType, StringComparison.OrdinalIgnoreCase))
metaData[Site] = ChinaType;
else
ThrowBadSiteException();
}
MetaSource.Create(),
MetaPages.Create(),
};

return TEMPLATE;
}

protected override AbstractSource DoCreateNewSource(string name, string url, List<MetaDataObject> templateAndValues)
{
string value;
int tmp;

MetaDataObject source = MetaDataObject.FindIn(templateAndValues, SmackProvider.META_SOURCE);
value = source.GetValueAsString();

if (String.IsNullOrEmpty(value))
throw new Exception("No source selected");

AbstractSource s = new SmackSource(value);

MetaDataObject pages = MetaDataObject.FindIn(templateAndValues, SmackProvider.META_PAGES);
value = pages.GetValueAsString();

if (String.IsNullOrEmpty(value))
tmp = DefaultPages;
else
ThrowBadSiteException();
int.TryParse(value, out tmp);

var source = new GenericSource(metaData[Site], PROVIDER);
source.SetMetaData(metaData);
pages.SetValue(tmp);

s.SetMetaData(templateAndValues);

return source;
return s;

}

private void ThrowBadSiteException()
public override AbstractSource CastSource(GenericSource src)
{
throw new Exception("Site must be: " + String.Join(", ", ChinaType, JapanType));
return SmackSource.CreateFrom(src);
}

private const int MaxPages = 1;

protected override List<AbstractItem> GetNewItems(AbstractSource source)
{
List<AbstractItem> items = new List<AbstractItem>();
SmackSource js = SmackSource.CreateFrom(source);
return GetNewItems(js);
}

WebClient wc = new WebClient();
for (int p = 1; p <= MaxPages; p++)
private const string ChinaBase = "http://www.chinasmack.com/page/";
private const string JapanBase = "http://www.japancrush.com/page/";

private const string ChinaSelect = "//div[@class='excerpt-post-title']/a";
private const string JapanSelect = "//div[@class='excerpt-post-title']/a";


private List<AbstractItem> GetNewItems(SmackSource source)
{
string BaseUrl;
string Selector;

if (source.Source == SourceNames.ChinaSmack.ToString())
{
BaseUrl = ChinaBase;
Selector = ChinaSelect;
}
else if (source.Source == SourceNames.JapanCrush.ToString())
{
BaseUrl = JapanBase;
Selector = JapanSelect;
}
else
throw new Exception(source + "not accepted");

string url;
string page;
List<AbstractItem> items = new List<AbstractItem>();

switch (source.GetMetaDataValue(Site))
{
case JapanType:
url = JapanSite;
break;
case ChinaType:
default:
url = ChinaSite;
break;
}
WebClient wc = new WebClient();

url += "/page/" + p;
int max = Convert.ToInt32(source.Pages);
for (int p = 1; p <= max; p++)
{

string url = BaseUrl + p;
string page;

page = wc.DownloadString(url);

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(page);

var itemNodes = doc.DocumentNode.SelectNodes("//div[@id='archive']");
itemNodes = itemNodes[0].SelectNodes(".//li");

foreach (var node in itemNodes)
var itemNodes = doc.DocumentNode.SelectNodes(Selector);

foreach (var a in itemNodes)
{
try
{

var a = node.SelectSingleNode(".//div[@class='excerpt-post-title']/a");

string name = WebUtility.HtmlDecode(a.InnerText);
string link = a.Attributes["href"].Value;

Expand All @@ -121,17 +148,13 @@ protected override List<AbstractItem> GetNewItems(AbstractSource source)
}
}
}

return items;
}

public override void DoAction(AbstractItem item)
{
Process.Start(item.ActionContent);
}

public override SourceOptions GetSourceOptions()
{
return SourceOptions.CreateFromParameters(false, false);
}
}
}
Loading

0 comments on commit e4903bd

Please sign in to comment.