diff --git a/.vscode/launch.json b/.vscode/launch.json index 32f8c15..6704225 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,7 +5,9 @@ "version": "0.2.0", "configurations": [ { - "args": ["-pn"], // <--- ADD OR MODIFY THIS LINE + "args": [ + "-pn" + ], // <--- ADD OR MODIFY THIS LINE } ] } \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..5f425b9 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "dotnet.defaultSolution": "URLNotesGrabberCORE/URLNotesGrabberCORE.sln" +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..6edf439 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,41 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/URLNotesGrabberCORE.sln", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary;ForceNoAlign" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/URLNotesGrabberCORE.sln", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary;ForceNoAlign" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "--project", + "${workspaceFolder}/URLNotesGrabberCORE.sln" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/URLNotesGrabberCORE/.vscode/tasks.json b/URLNotesGrabberCORE/.vscode/tasks.json new file mode 100644 index 0000000..6edf439 --- /dev/null +++ b/URLNotesGrabberCORE/.vscode/tasks.json @@ -0,0 +1,41 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/URLNotesGrabberCORE.sln", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary;ForceNoAlign" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/URLNotesGrabberCORE.sln", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary;ForceNoAlign" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "--project", + "${workspaceFolder}/URLNotesGrabberCORE.sln" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/URLNotesGrabberCORE/DataAccess.cs b/URLNotesGrabberCORE/DataAccess.cs index c12f78c..e7fb1b3 100644 --- a/URLNotesGrabberCORE/DataAccess.cs +++ b/URLNotesGrabberCORE/DataAccess.cs @@ -194,10 +194,12 @@ namespace URLNotesGrabberCORE { connection.Open(); - string sql = "INSERT INTO DailyAPICount (Date) values('" + DateTime.Today.ToShortDateString() + "')"; - SQLiteCommand command = new SQLiteCommand(sql, connection); - - command.ExecuteNonQuery(); + string sql = "INSERT INTO DailyAPICount (Date) values(@date)"; + using (SQLiteCommand command = new SQLiteCommand(sql, connection)) + { + command.Parameters.AddWithValue("@date", DateTime.Today.ToShortDateString()); + command.ExecuteNonQuery(); + } } catch (Exception ex) { @@ -222,10 +224,17 @@ namespace URLNotesGrabberCORE { connection.Open(); - string sql = "INSERT INTO Notes (rootBlogName, noteBlogName, PostID, TimeStamp, Type) values('" + rootBlogName + "', '" + noteBlogName + "', " + postID + ", " + timestamp + ", '" + type + "')"; - SQLiteCommand command = new SQLiteCommand(sql, connection); + string sql = "INSERT INTO Notes (rootBlogName, noteBlogName, PostID, TimeStamp, Type) values(@rootBlogName, @noteBlogName, @PostID, @TimeStamp, @Type)"; + using (SQLiteCommand command = new SQLiteCommand(sql, connection)) + { + command.Parameters.AddWithValue("@rootBlogName", rootBlogName); + command.Parameters.AddWithValue("@noteBlogName", noteBlogName); + command.Parameters.AddWithValue("@PostID", postID); + command.Parameters.AddWithValue("@TimeStamp", timestamp); + command.Parameters.AddWithValue("@Type", type ?? string.Empty); - command.ExecuteNonQuery(); + command.ExecuteNonQuery(); + } } catch (Exception ex) { @@ -377,15 +386,11 @@ namespace URLNotesGrabberCORE { connection.Open(); - string sql = "SELECT " + - " APICount " + - "FROM " + - " DailyAPICount " + - " WHERE [Date] = '" + - DateTime.Today.ToShortDateString() + "'"; + string sql = "SELECT APICount FROM DailyAPICount WHERE [Date] = @date"; using (SQLiteCommand command = new SQLiteCommand(sql, connection)) { + command.Parameters.AddWithValue("@date", DateTime.Today.ToShortDateString()); using (SQLiteDataReader reader = command.ExecuteReader()) { while (reader.Read()) @@ -417,11 +422,15 @@ namespace URLNotesGrabberCORE connection.Open(); string sql = ""; if (reblogsOnly) - sql = "SELECT NoteBlogName as blogName, count(*) from notes inner join blogs on blogs.BlogName = notes.NoteBlogName where type IN ('reblog', 'reply', 'posted') and HasBeenOutput = 0 group by NoteBlogName ORDER BY count(*) desc, BlogName LIMIT " + top; + sql = "SELECT NoteBlogName as blogName, count(*) FROM notes INNER JOIN blogs ON blogs.BlogName = notes.NoteBlogName WHERE blogs.IsActive = @isActive AND type IN ('reblog', 'reply', 'posted') AND HasBeenOutput = 0 GROUP BY NoteBlogName ORDER BY count(*) DESC, BlogName LIMIT @top"; else - sql = "SELECT NoteBlogName as blogName, count(*) from notes inner join blogs on blogs.BlogName = notes.NoteBlogName where type NOT IN ('reblog', 'reply', 'posted') and HasBeenOutput = 0 group by NoteBlogName ORDER BY count(*) desc, BlogName LIMIT " + top; + sql = "SELECT NoteBlogName as blogName, count(*) FROM notes INNER JOIN blogs ON blogs.BlogName = notes.NoteBlogName WHERE blogs.IsActive = @isActive AND type NOT IN ('reblog', 'reply', 'posted') AND HasBeenOutput = 0 GROUP BY NoteBlogName ORDER BY count(*) DESC, BlogName LIMIT @top"; + using (SQLiteCommand command = new SQLiteCommand(sql, connection)) { + command.Parameters.AddWithValue("@isActive", 1); + command.Parameters.AddWithValue("@top", top); + using (SQLiteDataReader reader = command.ExecuteReader()) { while (reader.Read()) @@ -456,11 +465,15 @@ namespace URLNotesGrabberCORE connection.Open(); string sql = ""; if (reblogsOnly) - sql = "SELECT NoteBlogName as blogName, count(*) from notes inner join blogs on blogs.BlogName = notes.NoteBlogName where type IN ('reblog', 'reply', 'posted') and HasBeenOutput = 0 group by NoteBlogName ORDER BY count(*) desc, BlogName LIMIT " + top; + sql = "SELECT NoteBlogName as blogName, count(*) FROM notes INNER JOIN blogs ON blogs.BlogName = notes.NoteBlogName WHERE blogs.IsActive = @isActive AND type IN ('reblog', 'reply', 'posted') AND HasBeenOutput = 0 GROUP BY NoteBlogName ORDER BY count(*) DESC, BlogName LIMIT @top"; else - sql = "SELECT NoteBlogName as blogName, count(*) from notes inner join blogs on blogs.BlogName = notes.NoteBlogName where HasBeenOutput = 0 group by NoteBlogName ORDER BY count(*) desc, BlogName LIMIT " + top; + sql = "SELECT NoteBlogName as blogName, count(*) FROM notes INNER JOIN blogs ON blogs.BlogName = notes.NoteBlogName WHERE blogs.IsActive = @isActive AND HasBeenOutput = 0 GROUP BY NoteBlogName ORDER BY count(*) DESC, BlogName LIMIT @top"; + using (SQLiteCommand command = new SQLiteCommand(sql, connection)) { + command.Parameters.AddWithValue("@isActive", 1); + command.Parameters.AddWithValue("@top", top); + using (SQLiteDataReader reader = command.ExecuteReader()) { while (reader.Read()) @@ -497,10 +510,14 @@ namespace URLNotesGrabberCORE { connection.Open(); - string sql = "UPDATE Posts SET HasNotesGathered = 1, NotesGatheredDateTime = " + DateTimeOffset.UtcNow.ToUnixTimeSeconds() + " WHERE BlogName = '" + blogName + "' AND PostID = " + postID; - SQLiteCommand command = new SQLiteCommand(sql, connection); - - command.ExecuteNonQuery(); + string sql = "UPDATE Posts SET HasNotesGathered = 1, NotesGatheredDateTime = @notesGathered WHERE BlogName = @BlogName AND PostID = @PostID"; + using (SQLiteCommand command = new SQLiteCommand(sql, connection)) + { + command.Parameters.AddWithValue("@notesGathered", DateTimeOffset.UtcNow.ToUnixTimeSeconds()); + command.Parameters.AddWithValue("@BlogName", blogName); + command.Parameters.AddWithValue("@PostID", postID); + command.ExecuteNonQuery(); + } } catch (Exception ex) { @@ -521,10 +538,13 @@ namespace URLNotesGrabberCORE { connection.Open(); - string sql = "UPDATE Posts SET NotFound = 1 WHERE BlogName = '" + blogName + "' AND PostID = " + postID; - SQLiteCommand command = new SQLiteCommand(sql, connection); - - command.ExecuteNonQuery(); + string sql = "UPDATE Posts SET NotFound = 1 WHERE BlogName = @BlogName AND PostID = @PostID"; + using (SQLiteCommand command = new SQLiteCommand(sql, connection)) + { + command.Parameters.AddWithValue("@BlogName", blogName); + command.Parameters.AddWithValue("@PostID", postID); + command.ExecuteNonQuery(); + } } catch (Exception ex) { @@ -545,10 +565,14 @@ namespace URLNotesGrabberCORE { connection.Open(); - string sql = "UPDATE Posts SET postDate = '" + postDate + "' WHERE BlogName = '" + blogName + "' AND PostID = " + postID; - SQLiteCommand command = new SQLiteCommand(sql, connection); - - command.ExecuteNonQuery(); + string sql = "UPDATE Posts SET postDate = @postDate WHERE BlogName = @BlogName AND PostID = @PostID"; + using (SQLiteCommand command = new SQLiteCommand(sql, connection)) + { + command.Parameters.AddWithValue("@postDate", postDate ?? string.Empty); + command.Parameters.AddWithValue("@BlogName", blogName); + command.Parameters.AddWithValue("@PostID", postID); + command.ExecuteNonQuery(); + } } catch (Exception ex) { @@ -574,10 +598,15 @@ namespace URLNotesGrabberCORE { connection.Open(); - string sql = string.Format("UPDATE Notes SET timestamp = '{0}' WHERE rootBlogName = '{1}' AND noteBlogName = '{2}' AND PostID = '{3}'", timestamp, rootBlogName, noteBlogName, postID); - SQLiteCommand command = new SQLiteCommand(sql, connection); - - command.ExecuteNonQuery(); + string sql = "UPDATE Notes SET timestamp = @timestamp WHERE rootBlogName = @rootBlogName AND noteBlogName = @noteBlogName AND PostID = @postID"; + using (SQLiteCommand command = new SQLiteCommand(sql, connection)) + { + command.Parameters.AddWithValue("@timestamp", timestamp); + command.Parameters.AddWithValue("@rootBlogName", rootBlogName); + command.Parameters.AddWithValue("@noteBlogName", noteBlogName); + command.Parameters.AddWithValue("@postID", postID); + command.ExecuteNonQuery(); + } } catch (Exception ex) { @@ -603,30 +632,54 @@ namespace URLNotesGrabberCORE { connection.Open(); - string sql = "UPDATE Posts "; - sql += "SET postDate = " + Q(postDate) + ", "; - sql += "reblogURL = " + Q(reblogURL) + ", "; - sql += " postURL = " + Q(postURL) + ", "; - sql += " slug = " + Q(slug) + ", "; - sql += " reblogKey = " + Q(reblogKey) + ", "; - sql += " reblogName = " + Q(reblogName) + ", "; - sql += " summary = " + Q(summary) + ", "; - sql += " quote = " + Q(quote) + ", "; - sql += " body = " + Q(body) + ", "; - sql += " tags = " + Q(tags) + ", "; - sql += " link = " + Q(link) + ", "; - sql += " photoURL = " + Q(photoURL) + ", "; - sql += " photoCaption = " + Q(photoCaption) + ", "; - sql += " downloadedFiles = " + Q(downloadedFiles) + ", "; - sql += " audioCaption = " + Q(audioCaption) + ", "; - sql += " question = " + Q(question) + ", "; - sql += " answer = " + Q(answer) + ", "; - sql += " title = " + Q(title) + ", "; - sql += " hasImage = " + hasImage; - sql += " WHERE BlogName = " + Q(blogName) + " AND PostID = " + postID; - SQLiteCommand command = new SQLiteCommand(sql, connection); + string sql = "UPDATE Posts SET "; + sql += "postDate = @postDate, "; + sql += "reblogURL = @reblogURL, "; + sql += "postURL = @postURL, "; + sql += "slug = @slug, "; + sql += "reblogKey = @reblogKey, "; + sql += "reblogName = @reblogName, "; + sql += "summary = @summary, "; + sql += "quote = @quote, "; + sql += "body = @body, "; + sql += "tags = @tags, "; + sql += "link = @link, "; + sql += "photoURL = @photoURL, "; + sql += "photoCaption = @photoCaption, "; + sql += "downloadedFiles = @downloadedFiles, "; + sql += "audioCaption = @audioCaption, "; + sql += "question = @question, "; + sql += "answer = @answer, "; + sql += "title = @title, "; + sql += "hasImage = @hasImage "; + sql += " WHERE BlogName = @BlogName AND PostID = @PostID"; - command.ExecuteNonQuery(); + using (SQLiteCommand command = new SQLiteCommand(sql, connection)) + { + command.Parameters.AddWithValue("@postDate", postDate ?? string.Empty); + command.Parameters.AddWithValue("@reblogURL", reblogURL ?? string.Empty); + command.Parameters.AddWithValue("@postURL", postURL ?? string.Empty); + command.Parameters.AddWithValue("@slug", slug ?? string.Empty); + command.Parameters.AddWithValue("@reblogKey", reblogKey ?? string.Empty); + command.Parameters.AddWithValue("@reblogName", reblogName ?? string.Empty); + command.Parameters.AddWithValue("@summary", summary ?? string.Empty); + command.Parameters.AddWithValue("@quote", quote ?? string.Empty); + command.Parameters.AddWithValue("@body", body ?? string.Empty); + command.Parameters.AddWithValue("@tags", tags ?? string.Empty); + command.Parameters.AddWithValue("@link", link ?? string.Empty); + command.Parameters.AddWithValue("@photoURL", photoURL ?? string.Empty); + command.Parameters.AddWithValue("@photoCaption", photoCaption ?? string.Empty); + command.Parameters.AddWithValue("@downloadedFiles", downloadedFiles ?? string.Empty); + command.Parameters.AddWithValue("@audioCaption", audioCaption ?? string.Empty); + command.Parameters.AddWithValue("@question", question ?? string.Empty); + command.Parameters.AddWithValue("@answer", answer ?? string.Empty); + command.Parameters.AddWithValue("@title", title ?? string.Empty); + command.Parameters.AddWithValue("@hasImage", hasImage ? 1 : 0); + command.Parameters.AddWithValue("@BlogName", blogName); + command.Parameters.AddWithValue("@PostID", postID); + + command.ExecuteNonQuery(); + } } catch (Exception ex) @@ -648,10 +701,12 @@ namespace URLNotesGrabberCORE { connection.Open(); - string sql = "UPDATE Blogs SET HasBeenOutput = 1 WHERE BlogName = '" + blogName + "'"; - SQLiteCommand command = new SQLiteCommand(sql, connection); - - command.ExecuteNonQuery(); + string sql = "UPDATE Blogs SET HasBeenOutput = 1 WHERE BlogName = @BlogName"; + using (SQLiteCommand command = new SQLiteCommand(sql, connection)) + { + command.Parameters.AddWithValue("@BlogName", blogName); + command.ExecuteNonQuery(); + } } catch (Exception ex) { @@ -675,12 +730,13 @@ namespace URLNotesGrabberCORE { connection.Open(); - string sql = "UPDATE DailyAPICount SET APICount = " + APICount + - " WHERE [Date] = '" + - DateTime.Today.ToShortDateString() + "'"; - SQLiteCommand command = new SQLiteCommand(sql, connection); - - command.ExecuteNonQuery(); + string sql = "UPDATE DailyAPICount SET APICount = @APICount WHERE [Date] = @date"; + using (SQLiteCommand command = new SQLiteCommand(sql, connection)) + { + command.Parameters.AddWithValue("@APICount", APICount); + command.Parameters.AddWithValue("@date", DateTime.Today.ToShortDateString()); + command.ExecuteNonQuery(); + } } catch (Exception ex) { @@ -753,6 +809,7 @@ namespace URLNotesGrabberCORE if (deserializedResult != null) { myDeserializedClass = deserializedResult; + myDeserializedClass.rawJson = myJsonResponse; } } catch (Exception ex) diff --git a/URLNotesGrabberCORE/Program.cs b/URLNotesGrabberCORE/Program.cs index a405643..0b9e2e8 100644 --- a/URLNotesGrabberCORE/Program.cs +++ b/URLNotesGrabberCORE/Program.cs @@ -364,6 +364,7 @@ namespace URLNotesGrabberCORE else if (response.response == null) { Console.WriteLine("##### Response Null - API Failure? ###"); + Console.WriteLine($"Raw JSON: {response.rawJson}"); return "FAILURE"; } else if (response.response.notes == null) @@ -373,13 +374,7 @@ namespace URLNotesGrabberCORE } else { - if (response.response == null) - { - Console.WriteLine("response.response == null"); - return "FAILURE"; - } - - while (response.response != null + while (response.response != null && response.response._links != null && long.Parse(response.response._links.next.query_params.before_timestamp) >= post.Item3) { diff --git a/URLNotesGrabberCORE/Properties/launchSettings.json b/URLNotesGrabberCORE/Properties/launchSettings.json index 33c48f3..00c22f3 100644 --- a/URLNotesGrabberCORE/Properties/launchSettings.json +++ b/URLNotesGrabberCORE/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "URLNotesGrabberCORE": { "commandName": "Project", - "commandLineArgs": "-collect 1" + "commandLineArgs": "-collect 0" } } } \ No newline at end of file diff --git a/URLNotesGrabberCORE/ResponseNotes.cs b/URLNotesGrabberCORE/ResponseNotes.cs index 8c577b9..67296fb 100644 --- a/URLNotesGrabberCORE/ResponseNotes.cs +++ b/URLNotesGrabberCORE/ResponseNotes.cs @@ -80,5 +80,7 @@ namespace URLNotesGrabberCORE public string statusCode { get; set; } public int retryInSeconds { get; set; } + + public string rawJson { get; set; } } } diff --git a/URLNotesGrabberCORE/TL.db b/URLNotesGrabberCORE/TL.db index db2c0ba..94763ee 100644 Binary files a/URLNotesGrabberCORE/TL.db and b/URLNotesGrabberCORE/TL.db differ