#retrieve feeds from socialist and anti imperialist publications, and merge them into geographical feeds, ready for display online

#import classes
from classes.feed import Feed
from classes.feedprocessor import FeedProcessor
from classes.finalfeed import FinalFeed
from classes.database import DatabaseHandler

#create canadian feeds
socialistProject = Feed("Socialist Project")
socialistProject.setDisplayURL("socialistproject.ca")
socialistProject.setFeedURL("https://socialistproject.ca/feed/")

breachMedia = Feed("Breach Media")
breachMedia.setDisplayURL("breachmedia.ca")
breachMedia.setFeedURL("https://breachmedia.ca/feed/")

theMaple = Feed("The Maple")
theMaple.setDisplayURL("readthemaple.com")
theMaple.setFeedURL("https://www.readthemaple.com/rss/")

peoplesVoice = Feed("People's Voice")
peoplesVoice.setDisplayURL("pvonline.ca")
peoplesVoice.setFeedURL("https://pvonline.ca/feed/")

yclCA = Feed("Canadian Young Communist League")
yclCA.setDisplayURL("ry-jm.ycl-ljc.ca")
yclCA.setFeedURL("https://ry-jm.ycl-ljc.ca/feed/")

canadafeedslist = [socialistProject,breachMedia,theMaple,peoplesVoice,yclCA]

#create uk feeds
solidarityUK = Feed("Solidarity Post UK");
solidarityUK.setDisplayURL("solidaritypost.org");
solidarityUK.setFeedURL("https://solidaritypost.org/category/uk/feed/");

declassifieduk = Feed("Declassified UK")
declassifieduk.setDisplayURL("declassifieduk.org")
declassifieduk.setFeedURL("https://www.declassifieduk.org/feed/")

canary = Feed("The Canary")
canary.setDisplayURL("thecanary.co")
canary.setFeedURL("https://www.thecanary.co/feed/")

novara = Feed("Novara Media")
novara.setDisplayURL("novaramedia.com")
novara.setFeedURL("https://novaramedia.com/category/articles/feed/")

ukfeedslist = [solidarityUK,declassifieduk,canary,novara]

#create US feeds
leftVoice = Feed("LeftVoice")
leftVoice.setDisplayURL("leftvoice.org")
leftVoice.setFeedURL("https://www.leftvoice.org/category/labor-movement/feed/")

jacobin = Feed("Jacobin Magazine")
jacobin.setDisplayURL("jacobin.com")
jacobin.setFeedURL("https://jacobin.com/rss/")

truthout = Feed("Truth Out")
truthout.setDisplayURL("truthout.org")
truthout.setFeedURL("https://truthout.org/feed/")

liberation = Feed("Liberation News")
liberation.setDisplayURL("liberationnews.org")
liberation.setFeedURL("https://liberationnews.org/feed/")

fightBack = Feed("Fight Back! News")
fightBack.setDisplayURL("fightbacknews.org")
fightBack.setFeedURL("https://fightbacknews.org/feed/")

redPhoenix = Feed("Red Phoenix")
redPhoenix.setDisplayURL("redphoenix.news")
redPhoenix.setFeedURL("https://redphoenix.news/category/us-news/feed/")

solidarityPostUS = Feed("Solidarity Post US")
solidarityPostUS.setDisplayURL("solidaritypost.org")
solidarityPostUS.setFeedURL("https://solidaritypost.org/category/us/feed/")

usfeedslist = [leftVoice,jacobin,truthout,liberation,fightBack,redPhoenix,solidarityPostUS]

#create world feeds
breakthrough = Feed("Breakthrough News")
breakthrough.setDisplayURL("breakthroughnews.org")
breakthrough.setFeedURL("https://breakthroughnews.org/feed/")

mintpress = Feed("Mint Press News")
mintpress.setDisplayURL("mintpressnews.com")
mintpress.setFeedURL("https://www.mintpressnews.com/feed/")

grayzone = Feed("The Grayzone")
grayzone.setDisplayURL("thegrayzone.com")
grayzone.setFeedURL("https://thegrayzone.com/feed/")

blackagenda = Feed("Black Agenda Report")
blackagenda.setDisplayURL("blackagendareport.com")
blackagenda.setFeedURL("https://www.blackagendareport.com/feeds-story")

geopolitical = Feed("Geopolitical Economy Report")
geopolitical.setDisplayURL("geopoliticaleconomyreport.com")
geopolitical.setFeedURL("https://geopoliticaleconomy.com/feed/")

workersorg = Feed("Workers World")
workersorg.setDisplayURL("workers.org")
workersorg.setFeedURL("https://www.workers.org/feed/")

thecradle = Feed("The Cradle")
thecradle.setDisplayURL("thecradle.co")
thecradle.setFeedURL("https://thecradle.co/feed/")

chinaAcademy = Feed("China Academy")
chinaAcademy.setDisplayURL("thechinaacademy.org")
chinaAcademy.setFeedURL("https://thechinaacademy.org/feed/")

solidarityPostWorld = Feed("Solidarity Post World")
solidarityPostWorld.setDisplayURL("solidaritypost.org")
solidarityPostWorld.setFeedURL("https://solidaritypost.org/category/world/feed/")

worldfeedslist = [breakthrough,mintpress,grayzone,blackagenda,geopolitical,workersorg,thecradle,chinaAcademy,solidarityPostWorld]

#retrieve feeds
for feed in canadafeedslist + ukfeedslist + usfeedslist + worldfeedslist:
    processor = FeedProcessor(feed)
    
    processor.retrieveFeed()
    
    if feed.feedContent() != None:
        print("Feed retrieved")
        
        #populate item list in feed object
        processor.extractItems()
    
    else:
        print(feed.getName() + " feed empty or retrieve failed")
    
    #print items
#    for item in feed.getItems():
#        print("title: " + item.getTitle())
#        print("display url: " + item.getDisplayURL())
#        print("url: " + item.getURL())
#        print("timestamp: " + str(item.getTimestamp()))
        
#create, populate, and sort final feeds
canadafeed = FinalFeed()
for feed in canadafeedslist:
    for item in feed.getItems():
        canadafeed.addItem(item)
canadafeed.sort()

ukfeed = FinalFeed()
for feed in ukfeedslist:
    for item in feed.getItems():
        ukfeed.addItem(item)
ukfeed.sort()

usfeed = FinalFeed()
for feed in usfeedslist:
    for item in feed.getItems():
        usfeed.addItem(item)
usfeed.sort()

worldfeed = FinalFeed()
for feed in worldfeedslist:
    for item in feed.getItems():
        worldfeed.addItem(item)
worldfeed.sort()

fake = Item()
fake.setTitle("This is a Title")
fake.setURL("https://example.com/")
fake.setDisplayURL("example.com")
fake.setTimestamp(1234567891)

handler = DatabaseHandler()
handler.connect()
handler.insertItem(fake)
handler.disconnect()