|
| 1 | +import csv |
| 2 | +import sys |
| 3 | + |
| 4 | +csvFile = open('export.csv', 'w') |
| 5 | +csvWriter = csv.writer(csvFile, quoting=csv.QUOTE_MINIMAL) |
| 6 | +csvWriter.writerow(['title', 'username', 'password', 'notes']) |
| 7 | + |
| 8 | +importSocket = open(sys.argv[1], 'r') |
| 9 | +importFile = importSocket.readlines() |
| 10 | +importSocket.close() |
| 11 | + |
| 12 | +# Concatenate strings |
| 13 | +myList = [] |
| 14 | +temp = '' |
| 15 | + |
| 16 | +for line in importFile: |
| 17 | + if line != '\n': |
| 18 | + temp = temp + line |
| 19 | + elif temp != '': |
| 20 | + myList.append(temp) |
| 21 | + temp = '' |
| 22 | +if temp != '': |
| 23 | + myList.append(temp) |
| 24 | + |
| 25 | +# Create entry |
| 26 | +def constructLine(splittedItem): |
| 27 | + entry = [] |
| 28 | + notes = '' |
| 29 | + withoutUsername = True |
| 30 | + withoutPassword = True |
| 31 | + usernameAsEmail = False |
| 32 | + |
| 33 | + # Try to find `Username` |
| 34 | + for line in splittedItem: |
| 35 | + if line.startswith('Username'): |
| 36 | + withoutUsername = False |
| 37 | + entry.append(line.replace('Username ', '')) |
| 38 | + # If there is no `Username` field — try to find `E-Mail` and use it as `Username` |
| 39 | + if withoutUsername: |
| 40 | + for line in splittedItem: |
| 41 | + if (line.startswith('E-Mail') or line.startswith('Email') or line.startswith('E-mail')): |
| 42 | + withoutUsername = False |
| 43 | + usernameAsEmail = True |
| 44 | + entry.append(line.replace('E-Mail ', '').replace('Email ', '').replace('E-mail', '')) |
| 45 | + # If there is no `E-Mail` — field is filled with `none` |
| 46 | + if withoutUsername: |
| 47 | + entry.append('none') |
| 48 | + # Try to find `Password` |
| 49 | + for line in splittedItem: |
| 50 | + if line.startswith('Password'): |
| 51 | + withoutPassword = False |
| 52 | + entry.append(line.replace('Password ', '')) |
| 53 | + # If there is no `Password` — field is filled with `none` |
| 54 | + if withoutPassword: |
| 55 | + entry.append('none') |
| 56 | + # Everything else goes to `notes` section |
| 57 | + for line in splittedItem: |
| 58 | + if (not line.startswith('Password') and not line.startswith('Username')) and not ((line.startswith('E-Mail') or line.startswith('Email') or line.startswith('E-mail')) and usernameAsEmail): |
| 59 | + notes += line.replace('Url ', '').replace('E-Mail ', '').replace('Email ', '').replace('E-mail', '') |
| 60 | + entry.append(notes) |
| 61 | + return entry |
| 62 | + |
| 63 | +# Iterate array, close file |
| 64 | +for item in myList: |
| 65 | + splittedItem = item.split('\n') |
| 66 | + # First entry always a name |
| 67 | + name = splittedItem.pop(0) |
| 68 | + csvWriter.writerow([name] + constructLine(splittedItem)) |
| 69 | + |
| 70 | +csvFile.close() |
0 commit comments