from PyQt5.QtWidgets import QWidget, QVBoxLayout, QScrollArea
from PyQt5.QtCore import QTimer
from PyQt5.QtGui import QIcon
from PyQt5 import QtCore
from gtweet import StatusWidget
import http.client
import pickle


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
        self.hide()

        self.tweets = []

        self.fetch_tweets()
        self.tim = QTimer(self)
        self.tim.timeout.connect(self.fetch_tweets)
        self.tim.start(60000)

        self.show()

    def fetch_tweets(self):
        if len(self.tweets) == 0:
            try:
                f = open("last_tweet", 'r')
                id = int(f.read())
                print(id)
                f.close()
            except:
                id = 0
        else:
            id = self.tweets[-1].tid

        conn = http.client.HTTPConnection("127.0.0.2", 8080)
        conn.request("GET", "/status/from_id/" + str(id))
        resp = conn.getresponse()
        if resp.status == 200:
            tw = pickle.load(resp)
            tw.sort()
            for i in tw:
                self.addTweet(i)

    def initUI(self):
        self.setWindowTitle('Twitter Client')
        QIcon.setThemeName("Faenza-Dark")

        lay = QVBoxLayout(self)
        scr = QScrollArea(self)
        scr.setWidgetResizable(True)
        scr.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)

        lay2 = QVBoxLayout()
        self.setLayout(lay)
        placehold = QWidget()
        lay.addWidget(scr)
        scr.setWidget(placehold)
        placehold.setLayout(lay2)
        self.lay = lay2

        lay2.setSpacing(0)
        lay.setSpacing(0)
        lay.setContentsMargins(0, 0, 0, 0)

        self.show()

    def addTweet(self, tweet):
        widget = StatusWidget(tweet)

        self.tweets.append(widget)
        widget.delete_tweets.connect(self.deleteTweets)
        self.lay.addWidget(widget)

    def deleteTweets(self, string_id):
        id = int(string_id)
        for i in self.tweets:
            if i.tid <= id:
                self.lay.removeWidget(i)
                i.hide()
        f = open("last_tweet", 'w')
        f.write(string_id)
        f.close()

@@ -181,27 +181,24 @@
 %0Aimport 
-http.client
+requests
 %0Aimport 
@@ -834,41 +834,24 @@
     
-conn = http.client.HTTPConnection
+r = requests.get
 (%2212
@@ -861,46 +861,13 @@
 .0.2
-%22, 
+:
 8080
-)%0A        conn.request(%22GET%22, %22
 /sta
@@ -877,17 +877,16 @@
 /from_id
-/
 %22 + str(
@@ -894,42 +894,8 @@
 d))%0A
-        resp = conn.getresponse()%0A
     
@@ -906,18 +906,20 @@
 if r
-esp
 .status
+_code
  == 
@@ -957,11 +957,13 @@
 ad(r
-esp
+.data
 )%0A  

Move from http.client to requests