ajbryn 1 month ago
parent
commit
f7efd10461

BIN
__pycache__/article.cpython-314.pyc


BIN
__pycache__/generate_qr.cpython-314.pyc


BIN
__pycache__/get_antiwar.cpython-314.pyc


BIN
__pycache__/section.cpython-314.pyc


+ 7 - 0
article.py

@@ -0,0 +1,7 @@
+class Article:
+    def __init__(self, title, author, published, text, link):
+        self.title = title
+        self.author = author
+        self.published = published
+        self.text = text
+        self.link = link

+ 14 - 0
generate_qr.py

@@ -0,0 +1,14 @@
+import qrcode
+
+def generate_qr_code(text, size = 5, fill="black", back="white"):
+        
+    qr = qrcode.QRCode(
+        version=1,
+        error_correction=qrcode.constants.ERROR_CORRECT_L,
+        box_size=5,
+        border=0,
+    )
+    qr.add_data(text)
+    qr.make(fit=True)
+    img = qr.make_image(fill_color=fill, back_color=back)
+    return img.get_image()

File diff suppressed because it is too large
+ 77 - 0
get_antiwar.py


BIN
news.pdf


+ 152 - 0
papernews.py

@@ -0,0 +1,152 @@
+from fpdf import FPDF
+from generate_qr import generate_qr_code
+from section import Section
+from get_antiwar import get_antiwar_article
+import re
+
+
+class PDF(FPDF):
+    def header(self):
+        self.set_font("helvetica", style="B", size=15)
+        width = self.get_string_width(self.title) + 6
+        self.set_x((210 - width) / 2)
+        # self.set_draw_color(0, 80, 180)
+        # self.set_fill_color(230, 230, 0)
+        # self.set_text_color(220, 50, 50)
+        self.set_line_width(.1)
+        self.cell(
+            width,
+            9,
+            self.title,
+            border=1,
+            new_x="LMARGIN",
+            new_y="NEXT",
+            align="C",
+            fill=False,
+        )
+        self.ln(10)
+
+    def footer(self):
+        self.set_y(-15)
+        self.set_font("helvetica", style="I", size=8)
+        self.set_text_color(128)
+        self.cell(0, 10, f"Page {self.page_no()}", align="C")
+
+pdf = PDF()
+
+pdf.set_title("Paper News Test")
+
+pdf.add_page()
+# pdf.set_text_shaping(True)
+
+awa = get_antiwar_article()
+
+col_width = 60
+
+pdf.set_font('helvetica', style="b", size=12)
+
+top = pdf.get_y()
+left = pdf.get_x()
+
+pdf.multi_cell(
+    w=col_width,
+    h=5,
+    text=awa.title,
+    border=False,
+    new_x="LMARGIN",
+    new_y="NEXT")
+
+pdf.set_font("Helvetica", size=8)
+
+pdf.multi_cell(
+    w=col_width,
+    h=8,
+    text=awa.author,
+    border=False,
+    new_x="LMARGIN",
+    new_y="NEXT")
+
+pdf.multi_cell(
+    w=col_width,
+    h=8,
+    text=awa.published,
+    border=False,
+    new_x="LMARGIN",
+    new_y="NEXT")
+
+
+transchars = str.maketrans({"’": "\'", "“": "\"", "”": "\"", "–": "-"})
+awatext = awa.text.translate(transchars)[:1000]
+
+awatext = re.sub(r'[^a-zA-Z]+$', '', awatext)
+
+# while awatext and not awatext[-1].isalpha():
+#     awatext = awatext[:-1]
+
+
+pdf.multi_cell(
+    w=col_width,
+    h=None,
+    text=awatext + '...',
+    border=False,
+    new_x="LMARGIN",
+    new_y="NEXT")
+
+pdf.set_y(pdf.get_y() + 10)
+pdf.image(generate_qr_code(awa.link), x=pdf.get_x() + col_width/4, y=None, h=col_width/2)
+pdf.set_y(pdf.get_y() + 3)
+
+pdf.set_font('helvetica', size=8)
+pdf.multi_cell(
+    w=col_width,
+    h=None,
+    text='Read more at antiwar.com',
+    align="C",
+    border=False,
+    new_x="LMARGIN",
+    new_y="TOP")
+
+col_space=5
+pdf.set_xy(left + col_width + col_space,top)
+
+pdf.multi_cell(
+    w=col_width,
+    h=None,
+    text=awatext + '...',
+    border=False,
+    new_x="LMARGIN",
+    new_y="NEXT")
+
+pdf.set_xy(left + col_width*2 + col_space*2,top)
+
+pdf.multi_cell(
+    w=col_width,
+    h=None,
+    text=awatext + '...',
+    border=False,
+    new_x="LMARGIN",
+    new_y="NEXT")
+
+# box_section = Section(50,30,55,col_width)
+# pdf.rect(pdf.get_x(), pdf.get_y() + 4, box_section.h, box_section.w, style="D") ##,round_corners=False, corner_radius=0)
+
+pdf.output("news.pdf")
+
+
+
+
+# TABLE_DATA = (
+#     ("First name", "Last name", "Age", "City"),
+#     ("Jules", "Smith", "34", "San Juan"),
+#     ("Mary", "Ramos", "45", "Orlando"),
+#     ("Carlson", "Banks", "19", "Los Angeles"),
+#     ("Lucas", "Cimon", "31", "Angers"),
+# )
+
+# pdf.set_font("Times", size=16)
+# with pdf.table() as table:
+#     for data_row in TABLE_DATA:
+#         row = table.row()
+#         for datum in data_row:
+#             row.cell(datum)
+# pdf.output('table.pdf')

+ 6 - 0
section.py

@@ -0,0 +1,6 @@
+class Section:
+    def __init__(self, x, y, h, w):
+        self.x = x
+        self.y = y
+        self.h = h
+        self.w = w

Some files were not shown because too many files changed in this diff