Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
RSS Reader
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Johannes Grothe
RSS Reader
Commits
6c057c66
Commit
6c057c66
authored
4 years ago
by
Johannes Grothe
Browse files
Options
Downloads
Patches
Plain Diff
#29
Parser working again
parent
fae41843
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
RSS_Reader/FeedParser.swift
+36
-62
36 additions, 62 deletions
RSS_Reader/FeedParser.swift
with
36 additions
and
62 deletions
RSS_Reader/FeedParser.swift
+
36
−
62
View file @
6c057c66
...
...
@@ -150,16 +150,7 @@ class FeedParser {
return
ArticleData
(
article_id
:
art_data
!.
guid
,
title
:
art_data
!.
title
,
description
:
art_data
!.
description
,
link
:
art_data
!.
link
,
pub_date
:
art_pub_date
!
,
author
:
nil
,
parent_feed
:
nil
)
}
func
parseData
()
->
FetchedFeedInfo
?
{
if
data
==
nil
{
print
(
"Cannot parse: no data fetched"
)
return
nil
}
if
url_protocol
==
nil
||
main_url
==
nil
||
sub_url
==
nil
{
print
(
"Cannot parse: no legal url data found"
)
return
nil
}
func
parseChannel
(
_
channel_str
:
String
)
->
FetchedFeedInfo
?
{
struct
RSSChannelMeta
:
Codable
{
let
title
:
String
...
...
@@ -168,70 +159,26 @@ class FeedParser {
let
language
:
String
}
struct
RSSDoc
:
Codable
{
let
rss
:
String
}
struct
RSSChannel
:
Codable
{
let
channel
:
String
}
// RSS parse
guard
let
checked_data
=
data
!.
data
(
using
:
.
utf8
)
else
{
print
(
"Parsing failed: illegal encoding"
)
return
nil
}
let
rss_doc
=
try
?
XMLDecoder
()
.
decode
(
RSSDoc
.
self
,
from
:
checked_data
)
if
rss_doc
==
nil
{
print
(
"Parsing failed: no legal rss feed document"
)
print
(
data
!
)
return
nil
}
if
rss_doc
!.
rss
==
""
{
print
(
"Parsing failed: empty rss feed document"
)
return
nil
}
// RSS parse end
// Channel parse
guard
let
checked_rss
=
rss_doc
!.
rss
.
data
(
using
:
.
utf8
)
else
{
// Meta parse
guard
let
checked_channel_str
=
channel_str
.
data
(
using
:
.
utf8
)
else
{
print
(
"Parsing failed: illegal encoding"
)
return
nil
}
let
channel_data
=
try
?
XMLDecoder
()
.
decode
(
RSSChannel
.
self
,
from
:
checked_
rss
)
let
channel_data
=
try
?
XMLDecoder
()
.
decode
(
RSSChannel
Meta
.
self
,
from
:
checked_
channel_str
)
if
channel_data
==
nil
{
print
(
"Parsing failed: no channel found"
)
return
nil
}
if
channel_data
!.
channel
==
""
{
print
(
"Parsing failed: empty channel found"
)
return
nil
}
// Channel parse end
// Meta parse
guard
let
checked_channel
=
channel_data
!.
channel
.
data
(
using
:
.
utf8
)
else
{
print
(
"Parsing failed: illegal encoding"
)
return
nil
}
let
meta_data
=
try
?
XMLDecoder
()
.
decode
(
RSSChannelMeta
.
self
,
from
:
checked_channel
)
if
meta_data
==
nil
{
print
(
"Parsing failed: couldn't parse meta data"
)
return
nil
}
// Meta parse end
var
article_data
:
[
ArticleData
]
=
[]
// Load Feeds
let
resul
t
=
getRegexMatches
(
for
:
"<item(.+?)</item>"
,
in
:
data
!
)
if
!
resul
t
.
isEmpty
{
for
article_str
in
resul
t
{
let
article_data_lis
t
=
getRegexMatches
(
for
:
"<item(.+?)</item>"
,
in
:
data
!
)
if
!
article_data_lis
t
.
isEmpty
{
for
article_str
in
article_data_lis
t
{
let
buf_art
=
parseArticle
(
article_str
)
if
buf_art
!=
nil
{
...
...
@@ -242,10 +189,37 @@ class FeedParser {
print
(
"No article data fetched"
)
}
let
news_feed
=
NewsFeedMeta
(
title
:
meta
_data
!.
title
,
description
:
meta
_data
!.
description
,
language
:
meta
_data
!.
language
,
url_protocol
:
url_protocol
!
,
main_url
:
main_url
!
,
sub_url
:
sub_url
!
)
let
news_feed
=
NewsFeedMeta
(
title
:
channel
_data
!.
title
,
description
:
channel
_data
!.
description
,
language
:
channel
_data
!.
language
,
url_protocol
:
url_protocol
!
,
main_url
:
main_url
!
,
sub_url
:
sub_url
!
)
return
FetchedFeedInfo
(
feed_info
:
news_feed
,
articles
:
article_data
)
}
func
parseData
()
->
FetchedFeedInfo
?
{
if
data
==
nil
{
print
(
"Cannot parse: no data fetched"
)
return
nil
}
if
url_protocol
==
nil
||
main_url
==
nil
||
sub_url
==
nil
{
print
(
"Cannot parse: no legal url data found"
)
return
nil
}
let
channel_str_list
=
getRegexMatches
(
for
:
"<channel(.+?)</channel>"
,
in
:
data
!
)
if
!
channel_str_list
.
isEmpty
{
if
channel_str_list
.
endIndex
==
1
{
let
channel_data
=
parseChannel
(
channel_str_list
[
0
])
return
channel_data
}
else
{
// TODO: handle
print
(
"Found multiple channels in one feed"
)
}
}
else
{
print
(
"No channel data found"
)
}
return
nil
}
}
struct
FetchedFeedInfo
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment