from bs4 import BeautifulSoup
from datetime import timedelta
import urllib3, sys, datetime

# static fields
hdrs = {'User-Agent' : 'Magic Browser'}
baseUrl = "http://www.pietsmiet.de/news/uploadplan"
urlUploadToday = "-upload-plan-am-"
index = len(baseUrl)-baseUrl.index("/news/uploadplan")
endQuote = "Alle Ang"

# changing fields
today = datetime.date.today()
date = "{:%d-%m-%Y}".format(today)
urlUploadTodayDate = urlUploadToday+date
title = "Upload-Plan am {:%d.%m.%Y}:".format(today)

# command line arguments
argument = sys.argv
argumentLen = len(sys.argv)

def correctDate(amountDays):
	global today, date, urlUploadTodayDate, title
	today = datetime.date.today()-timedelta(days=amountDays)
	date = "{:%d-%m-%Y}".format(today)
	urlUploadTodayDate = urlUploadToday+date
	title = "Upload-Plan am {:%d.%m.%Y}:".format(today)

def checkArguments():
	if(argumentLen < 2):
		outputUploadPlan()
	elif (argumentLen >= 2):
		if (argument[1] in("yesterday", "y")):
			correctDate(1)
			outputUploadPlan()

def outputUploadPlan():
	try:
		print (getList())
	except Exception as e:
		print ("No Upload-Plan yet...")

def getList():
	sp = getSoupOf(getUrlContent(getUrlUploadPlan())).prettify()
	start = sp.find(title)
	end = len(sp)-sp.find(endQuote)
	return getSoupOf(sp[start:-end]).get_text()

def getUrlContent(url):
	http = urllib3.PoolManager()
	req = http.request('GET', url)
	return req.data

def getSoupOf(urlContent):
	return BeautifulSoup(urlContent, 'html.parser')

def getUrlUploadPlan():
	for link in getSoupOf(getUrlContent(baseUrl)).find_all('a'):
		if "readmore-link" in str(link.get('class')) and urlUploadTodayDate in str(link.get('href')):
			return baseUrl[0:-index]+link.get('href')

if __name__ == '__main__':
	checkArguments()
@@ -64,17 +64,8 @@
 port
- urllib3,
  sys
@@ -74,16 +74,26 @@
 datetime
+, requests
 %0A%0A# stat
@@ -360,36 +360,34 @@
 y()%0Adate = %22%7B:%25d
--
 %25m
--
 %25Y%7D%22.format(toda
@@ -708,12 +708,10 @@
 %7B:%25d
--
 %25m
--
 %25Y%7D%22
@@ -1311,65 +1311,28 @@
 l):%0A
-%09http = urllib3.PoolManager()%0A
 %09req = 
-http.
 request
-('GET', 
+s.get(
 url)
@@ -1348,12 +1348,12 @@
 req.
-data
+text
 %0A%0Ade
@@ -1522,54 +1522,8 @@
 %09if 
-%22readmore-link%22 in str(link.get('class')) and 
 urlU
@@ -1577,26 +1577,8 @@
 urn 
-baseUrl%5B0:-index%5D+
 link

Update requests to requests library